U
U
UniProger2021-08-05 21:04:22
Unity
UniProger, 2021-08-05 21:04:22

How to remove object sliding when moving through Rigidbody MovePosition?

Hello, there is a Top Down project with the following movement implementation:

[RequireComponent(typeof(Rigidbody2D))]
public class Player : MonoBehaviour
{
    [SerializeField] private float _speed;

    private Rigidbody2D _rigidbody;
    private Vector2 _movement;

    private void Start()
    {
        _rigidbody = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        _movement.x = Input.GetAxis("Horizontal");
        _movement.y = Input.GetAxis("Vertical");

    }

    private void FixedUpdate()
    {
        _rigidbody.MovePosition(_rigidbody.position + _movement * _speed * Time.fixedDeltaTime);

    }
}


So when I press the arrows and the movement should stop, the character moves further by inertia, how to remove it?
And another question, is this the optimal solution for managing a Top Down game?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom, 2021-08-06
Maidurov @sylniyduxom

In the Rigidbody settings, you can increase the Drag (resistance) or Mass (mass) parameter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question