0
votes

So basically I created a simple 3d scene in unity but my "character" keeps falling through objects or pass through them and i tried every type of collider(mesh,box,terrain) even tried adding a rigidbody component to each thing but it didnt do the trick

Here is My code for making the "character" move


public class PlayerMovement : MonoBehaviour
{
    //
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -19.8f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;
    

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded && velocity.y < 0)
        {
            velocity.y = 0f;
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
        
        if(Input.GetKey(KeyCode.LeftShift))
        {
            speed = 24;
        }
        else
        {
            speed = 12;
        }

        if(Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

    }
}
2
Is any collider marked as IsTrigger ? And have you checked the Physics settings -> Layer Collision Matrix ? maybe your layers ignore each other? - derHugo

2 Answers

0
votes

So first, anything that you manipulate directly with transform position will go through other objects but I can see that you are not doing that - at least in this code. You are correctly asking the character controller to move and it looks similar to the example given in the Unity documentation here: https://docs.unity3d.com/ScriptReference/CharacterController.Move.html Here is a long explanation of how the Character Controller works: https://medium.com/ironequal/unity-character-controller-vs-rigidbody-a1e243591483

Basic colliders are the best to start with, but a Character Controller already has a capsule collider with it. Typically you would add a Rigidbody to the player if you were doing your own custom controller and movement code, and that would satisfy the requirement that one collider must have a rigidbody on it for physics to affect it and cause collisions. Checking the Physics Matrix in the Project settings is a good place to start, but if you just created a new project then I doubt that is the problem. Make sure your ground/terrain has a box collider defining the flat ground.

My suggestion is to just use the Unity Third Person controller as a starting point: https://assetstore.unity.com/packages/essentials/starter-assets-third-person-character-controller-196526

A full explanation of what colliders will collider with each other and when you need a rigidbody is here: https://docs.unity3d.com/Manual/CollidersOverview.html

0
votes

Most probably, as a character is a complex mesh, you picked Mesh Collider AND forgot to check the convex checkbox... Needed since Unity 5.

enter image description here