- Home /
Player rotates immediately
Hey guys, every time when I press play in Unity, my player rotates immediately for no reason. Does someone know how I can fix that?
Answer by NaturedOne · Feb 26 at 08:19 AM
Sounds like you're using a rigidbody which means your character controller is interacting with the world via the physics system which is probably why it's rotating on its own.
Thank you very much for the answer! How do I can fix that?
Well, I guess it depends on what you want. You can look at the Rigid Body in the inspector and expand the "constraints" menu and then check the box that freezes Z axis rotation (if your game is 2D)
This might help you achieve the result you want.
I am working on a 3D game, so the player has to turn in all four directions
You want to at least freeze the X and Z rotation. This will keep you character from falling over. I usually freeze the Y rotation as well, cause then the rigidbody won't rotate unexpectedly but can still be rotated via player input.
Answer by antonsimmerle · Feb 26 at 09:57 AM
Now when I start the game and press a key, the player rotates, but when I press the same key again, the player stops rotating and moves like normal. Do you know why?
When I activate Kinematics the Movement works, but the gravity is not working. Is there a other way to prevent the player from falling over, instead of freezing the rotation?
My Movement Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public CharacterController controller;
public float speed = 6f;
// Start is called before the first frame update void Start() {
}
void Update()
{
float vertical = Input.GetAxisRaw("Vertical");
float horizontal = Input.GetAxisRaw("Horizontal");
Vector3 direction = new Vector3(vertical, 0f, -horizontal).normalized;
if (direction.magnitude >= 0.1)
{
float targetAngle = Mathf.Atan2(direction.x, -direction.z) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, -targetAngle + 191f, 0f);
controller.Move(direction * speed * Time.deltaTime);
}
}
}