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: