1
\$\begingroup\$

I've made a very simple 2D top down prototype with Unity. This is the code I use for the player:

public class PlayerTurnsController : MonoBehaviour
{
    private Rigidbody2D m_rb;
    public float Speed;

    Vector2 m_movement;

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

    private void Update()
    {
        var moveHorizontal = Input.GetAxis("Horizontal");
        var moveVertical = Input.GetAxis("Vertical");
        m_movement = new Vector2(moveHorizontal, moveVertical);
    }

    void FixedUpdate()
    {
        m_rb.MovePosition(m_rb.position + m_movement * Speed * Time.fixedDeltaTime);
    }
}

At the start and while moving everything seems OK. But when I bump into some Colliders, the player sprite starts spinning when it should just stop (not pressing any button anymore)

I already tried:

  • To set the rotation only when the m_Movement-Vector is > 0.1f. But this didn't change anything.
\$\endgroup\$
3
  • \$\begingroup\$ Remember that the documentation is freely available online. Happily, two of the most convenient properties to use to solve this problem are the very first two listed on that page (not to mention freezeRotation and AddTorque a little lower down). I'd recommend doing a bit more searching and experimentation in future. Trust in your own ability to solve problems. You got this! :) \$\endgroup\$ – DMGregory Jul 5 '20 at 17:58
  • \$\begingroup\$ @DMGregory: This might not be the right place for this discussion but isnt this the place to ask exactly this kind of questions? At least on stack overflow there are a lot of questions like: "What is a NullPointerException, and how do I fix it?" where the answer could be found in the documentation too. \$\endgroup\$ – Gener4tor Jul 5 '20 at 18:31
  • \$\begingroup\$ While not off-topic (hence no close votes), questions here are expected to demonstrate research effort, just like on StackOverflow. Questions that don't show at least a review of the relevant documentation tend to get down-voted. That's why I try to hint users toward useful research threads, so they can practice the skills needed to ask higher-quality questions that attract good answers rather than down-votes. \$\endgroup\$ – DMGregory Jul 5 '20 at 19:02
0
\$\begingroup\$

With the help of DMGregory I solved my problem.

The way I chose is to set "Freeze Rotation Z" of the Regidbody 2D via the inspector.

An other way is to increase the angularDrag of the Regidbody 2D via the inspector to e.g. 10.

This works too:

void Start()
{
    m_rb = GetComponent<Rigidbody2D>();
    m_rb.freezeRotation = true;
}

Also does:

void FixedUpdate()
{
    m_rb.MovePosition(m_rb.position + m_movement * Speed * Time.fixedDeltaTime);
    m_rb.angularVelocity = 0;
}

Im not sure which method is the best or which one to use when, but for the moment I chose "Freeze Rotation Z" = true in the inspector.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.