Stop 2D player object spinning when hitting other colliders?

I’ve seen this issue posted by other people, but I’ve tried their solutions with no luck! Looking for any help I can get. I’m brand new to Unity and have no formal training (Youtube and Unity manuals are my friends lol).

I have a 2D top down game with a player game object that has both a Box Collider 2D and a Rigidbody 2D. I control the player with the script info posted below. I’ve tried lots of other methods, and this one seems to work the best compared to other methods which all have the same issue.

When my player character runs into any other collider, it starts to spin and turn in the game world.

I’ve tried locking the Z constraints on the Rigidbody, but they don’t work. I’ve tried this in both the inspector and by scripting it…

I’ve tried scripting for there to be zero rotation on the rigidbody, but it ignores that as well.

I know one issue is that upon pushing play/test, my character’s inertia goes to 0.1629409 for some reason. I tried scripting that to always be 0 and that doesn’t stop it.

Here’s my script:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Animations;

public class PlayerScript : MonoBehaviour
{
    //for movement methods
    public Rigidbody2D rb2D;
    public float runSpeed = 3.0f;
    public Vector2 movement = new Vector2();
    public GameObject computerScreen;
    public bool isHoldingItem;

    //gets the keyboard commands for movement
    private void GetInput()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");
    }

    //moves the character according to keyboard commands
    public void MoveCharacter(Vector2 movementVector)
    {
        movementVector.Normalize();
        rb2D.velocity = movementVector * runSpeed;
    }


    void FixedUpdate()
    {
        GetInput();
        MoveCharacter(movement);
    }
}

Video of the issue:

Can you not just use the FreezeRotation checkbox?

3 Likes

I tried, but when I click play it automatically un-checks itself! :frowning: I even tried to type a line of code in the update method to use the RigidBody2D constraints to prohibit rotation and that didn’t work. I’m at a total loss as to how I’ve broken it so badly :hushed:

Sounds like you’ve still got the constraints in the scripts running: https://docs.unity3d.com/ScriptReference/Rigidbody2D-constraints.html

3 Likes

You were 100% right! XD I went through every script and found where I had accidentally left in a line setting the constraints to “none”. Deleted that and it works now!

2 Likes

Bro i found out, if your player is rotating, try to turn off the rotation in Rigidbody2D settings
9014299--1242874--upload_2023-5-15_15-12-34.png

9014299--1242874--upload_2023-5-15_15-12-34.png

3 Likes

thanks bro it helped me