x


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.

more ▼

asked Mar 09 '10 at 12:50 AM

Garrett Chase 3 gravatar image

Garrett Chase 3
47 7 7 7

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
}
more ▼

answered Mar 09 '10 at 03:52 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.5k 20 27 71

Thanks, this should work. I just have to do a little tuning because for some reason my model's axis' are messed up.

Mar 11 '10 at 03:00 AM Garrett Chase 3
(comments are locked)
10|3000 characters needed characters left

It is not working for me. Any suggestions?

more ▼

answered Mar 19 '10 at 10:57 AM

mike1602 gravatar image

mike1602
17 2 3

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.

Mar 19 '10 at 11:57 AM Ricardo

@mike1602 and this is not exactly the right spot to ask questions.. this area is only for answers.. so consider adding comment instead if you need more details on the same question... :)

Mar 19 '10 at 12:47 PM Lipis
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2244
x1422
x702
x584
x128

asked: Mar 09 '10 at 12:50 AM

Seen: 2739 times

Last Updated: Mar 09 '10 at 11:08 AM