Majs
Majs

Reputation: 56

Moving Rigidbody using player input

Im trying to move an object in a 3d world using a controller, but think I am missing something cus it just clips away as soon as I give any input:

private void Update()
{
    float h = Input.GetAxisRaw("Horizontal");
    float v = Input.GetAxisRaw("Vertical");

    Vector3 movement = new Vector3(h, 0, v).normalized * Time.deltaTime * speed;
    if(h != 0 || v != 0)
        _rigidBody.MovePosition(movement);
}

Works fine using _rigidbody.velocity, but as I understand it that should be avoided for these types of things.

Upvotes: 0

Views: 232

Answers (2)

Nikola G.
Nikola G.

Reputation: 335

"using a controller"

Do you mean Character Controller Component? You have 2 options and they are both well explained in Unity Documentation. Second is by Rigidbody Component.

Upvotes: 0

Julxzs
Julxzs

Reputation: 737

Rigidbody.MovePosition sets the position of the rigidbody with interpolation. It looks like you want to offset the position by movement, so you should probably set the velocity. If you do still want to use MovePosition, you should do _rigidBody.MovePosition(transform.position + movement);.

Upvotes: 2

Related Questions