- Home /
How can I get my model to face the direction it is moving?
I want my model to face the direction that it is moving in. Does anyone have some good code to do this? Here is the code that I am using to move the model:
moveDirection = new Vector3((Input.GetAxis("Horizontal")), (-Input.GetAxis("Vertical")), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
I want the model to rotate and face the direction that it is moving. Like I said if anyone has any good code for doing this, I would really appreciate it.
Answer by Jaap Kreijkamp · Mar 09, 2010 at 03:52 AM
A simple way is to use LookAt
. So this would give something like:
moveDirection = new Vector3((Input.GetAxis("Horizontal")), (-Input.GetAxis("Vertical")), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (moveDirection.sqrMagnitude > 0f) {
transform.LookAt(transform.position + moveDirection, Vector3.forward);
}
Thanks, this should work. I just have to do a little tuning because for some reason my model's axis' are messed up.
Answer by mike1602 · Mar 19, 2010 at 10:57 AM
It is not working for me. Any suggestions?
As your question will move up and down depending on the votes, you'll need to add more details on what exactly isn't working for you for someone to be able to help.
@mike1602 and this is not exactly the right spot to ask questions.. this area is only for answers.. so consider adding comment ins$$anonymous$$d if you need more details on the same question... :)
Your answer
Follow this Question
Related Questions
2D sprite rotating in 3D plane 0 Answers
[2D] CharacterController rotation 0 Answers
Top-Down Movement in Unity 1 Answer
How to Logically Match Ground Slope While Using This Code? 1 Answer
Rotate character to the moving direction problems? 2 Answers