- Home /
Question by
Rs31412 · Feb 22, 2014 at 04:04 AM ·
rotationrigidbodyquaternionvelocity
Rotation based on Velocity returning 0,0,0 rotation
Hello, I recently started coding for a game I'm working on and I noticed when I moved and my Player rotated that if i stopped putting in an input it set rotation to 0,0,0 and the console spat out this error:
UnityEngine.Quaternion:LookRotation(Vector3, Vector3) PlayerController:FixedUpdate() (at Assets/scripts/PlayerController.cs:25) This is my code: void FixedUpdate () { transform.rigidbody.constraints = RigidbodyConstraints.FreezePositionY; float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis ("Vertical");Look rotation viewing vector is zero
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement speed Time.deltaTime);
transform.rotation = Quaternion.LookRotation((rigidbody.velocity), Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, transform.rotation, Time.deltaTime);
Vector2 inputAmounts = new Vector2 (Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if(inputAmounts == Vector2.zero) { rigidbody.drag = 100f; } else rigidbody.drag = 0f; } The line returning the issue is transform.rotation = Quaternion.LookRotation((rigidbody.velocity), Vector3.up); I just want to know how to set it to the previous rotation before the velocity hits 0,0,0.
Comment
Best Answer
Answer by robertbu · Feb 22, 2014 at 04:05 AM
Just don't set a new rotation if Rigidbody.velocity is Vector3.zero:
if (rigidbody.velocity != Vector3.zero) {
transform.rotation = Quaternion.LookRotation((rigidbody.velocity), Vector3.up);
}
One more question to follow up on this! How would I slowly change the rotation from it's current rotation to the new rotation if the velocity equals zero?