- Home /
Change character's Y rotation based on velocity.
I'm making a top down puzzle game. You can move the character up down left and right. The character is moved by changing its velocity (rigidbody.velocity = movement). The only thing I need to do now is rotate him, I've tried LookAt, LookRotation, transform.rotation and SetLookRotation but they all seem to bug out or just not work at all. I just want the characters Y rotation to change based on its velocity, if its moving up, then rotate his Y in that direction.
My Code: public float speed = 6f; public GameObject camTarget;
Vector3 movement;
Rigidbody playerRigidbody;
void Awake(){
playerRigidbody = GetComponent<Rigidbody> ();
}
void FixedUpdate(){
if(Input.GetKeyDown(KeyCode.E)){
CameraShake();
}
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
Move (h, v);
}
void Move(float h, float v){
movement.Set (h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.velocity = movement;
}
void CameraShake(){
Camera.main.transform.GetComponent<Animation> ().Play ("CameraDarken");
camTarget.GetComponent<Animation> ().Play ("CameraShake");
}
My Character:
Any help at all would be greatly appreciated.
You're using a rigidbody, have you tried AddTorque or AddRelativeTorque?
http://docs.unity3d.com/ScriptReference/Rigidbody.AddTorque.html
http://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeTorque.html
Answer by zach-r-d · Aug 02, 2015 at 07:08 PM
Try putting this right after setting the velocity:
playerRigidbody.MoveRotation(Quaternion.LookRotation(movement));
You're welcome, glad it works! And remember to accept an answer if it answers your question, you get karma for it!
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
Character Rotation 2 Answers
Character MoveLook rotation locked? 1 Answer
How to rotate Character on Y axis along with the mouse rotation? 0 Answers