- Home /
3rd person character spinning fast when rotating while standing still
Hi everybody,
I've got a little problem. Whenever my character moves position (like walking), the rotation works like it should. But whenever the character tries to rotate whenever it isn't moving, it spins out like crazy. This also happens whenever it tries to walk backwards.
It seems that whenever it rotation while not moving, it turns 90 degrees (everytime the rotation is being called) and 180 degrees when trying to walk backwards.
I do have a character controller attached with the default settings and I'm using the following script.
public class ThirdPersonMoveScript : MonoBehaviour
{
public float walkSpeed = 3f;
public float runSpeed = 6f;
public float mouseSenitivity = 100f;
private Vector3 moveDirection = Vector3.zero;
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(Input.GetAxis("Mouse X") * mouseSenitivity, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
if (Input.GetKey(KeyCode.LeftShift))
{
moveDirection *= runSpeed;
}
else
{
moveDirection *= walkSpeed;
}
if (moveDirection != Vector3.zero)
{
//transform.rotation = Quaternion.LookRotation(moveDirection);
controller.transform.rotation = Quaternion.LookRotation(moveDirection);
}
controller.Move(moveDirection * Time.deltaTime);
}
}
Is anyone familiar with this problem?
Thanks upfront,
Jenoah
Answer by Eno-Khaon · Apr 28, 2018 at 07:56 PM
controller.transform.rotation = Quaternion.LookRotation(moveDirection);
By having your character attempt to face the direction they're moving, this causes them to turn around and face that way while attempting to move backwards. Because the direction you're moving at this point will keep changing relative to the direction you're now facing, you will wind up spinning wildly (at best) by attempting to move backwards.
Furthermore, when not moving, Unity's struggling/failing to determine a proper facing when the LookRotation vector provided is (0, 0, 0).
Your answer
Follow this Question
Related Questions
How to disable A, S, and D keys for third person player movement 1 Answer
Slowly move a GameObject on 1 axis, then destroy it. 1 Answer
2D Topdown Character Contoller 0 Answers
Slime script? 1 Answer