Home > Net >  How to prevent the player from going on top of object when moving?
How to prevent the player from going on top of object when moving?

Time:06-05

The player goes on top of some objects when he walks toward them, how can I prevent that from happening? Here is an example image of that:

enter image description here

I did not jump to be on the couch but yet it still goes on top of it when I walk to it. Here is my player information:

enter image description here

I don't want to change the player's movement, but I don't want it to go on top of objects when I'm walking.

CodePudding user response:

Make the Step Offset in character controller 0. More info on it enter image description here

To solve the problem of gravity, it is enough to first get the component.

private CharacterController controller;
public void Start()
{
    controller = GetComponent<CharacterController>();
}

And then apply gravity to the object with the following instructions, you have already obtained the moveInput axis with the Input.GetAxis method.

private Vector3 velocity;
private void Update()
{
    var moveInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

    // === AFTER CALCULATING MOVE INPUT
    
    controller.Move(moveInput*Time.deltaTime);

    velocity  = Physics.gravity * Time.deltaTime;

    controller.Move(velocity); // Apply Gravity

    if (controller.isGrounded) velocity = Vector3.zero;
}
  • Related