Home > OS >  Character not colliding with game objects when using Character Controller
Character not colliding with game objects when using Character Controller

Time:01-08

I am creating a runner game where you have a character which runs forward and you have to dodge obstacles. However my character doesn't collide with anything but passes through them.

Here is my code which controls my characters movement (the forwards movement is done automatically and the player controls sideways movement):


public class CharacterControllertest : MonoBehaviour
{
    public float sidewaysSpeed = 5.0f;
    public float forwardSpeed = 50.0f;
    Vector3 target;

    private void Start()
    {
        SetNewTarget(new Vector3(
            transform.position.x,
            transform.position.y,
            transform.position.z   10
        ));
    }
    // Update is called once per frame
    void Update()
    {
        Vector3 direction = target - transform.position;
        transform.Translate(direction.normalized * forwardSpeed * Time.deltaTime, Space.World);
        SetNewTarget(new Vector3(
            transform.position.x,
            transform.position.y,
            transform.position.z   10
        )); ;

        float translation = Input.GetAxis("Horizontal") * sidewaysSpeed;

        translation *= Time.deltaTime;

        transform.Translate(translation, 0, 0);
    }
    void SetNewTarget(Vector3 newTarget)
    {
        target = newTarget;
            transform.LookAt(target);
    }
} ```

CodePudding user response:

One of the reasons your player might not be colliding with anything is because there is no collider on it. If you add the appropriate collider or a mesh collider, you should start having collisions. Another reason is no colliders on the things you are trying to stand on. Try adding colliders to your objects and you should be good! :)

CodePudding user response:

Whenever physics is involved you do not want to move anything using Transform in Update. This causes unnatural movement and breaks with physics and collision detection.

If you are using the CharacterController, then actually use it and instead of Translate use CharacterController.Move

A CharacterController is not affected by forces and will only move when you call the Move function. It will then carry out the movement but be constrained by collisions.

So something like e.g.

// Reference via the Inspector
[SerializeField] private CharacterController _characterController;

private void Awake ()
{
    // Or as fallback get once
    if(!_characterController) _characterController = GetComponent<CharacterController>();
}

private void Update ()
{
    var direction = (target - transform.position).normalized;

    _characterController.Move(direction * forwardSpeed * Time.deltaTime);
    SetNewTarget(transform.position   Vector3.forward * 10);

    var horizontal = Input.GetAxis("Horizontal") * sidewaysSpeed * Time.deltaTime;

    _characterController.Move(Vector3.right * horizontal);
}
  •  Tags:  
  • Related