- Home /
How can I get my character to face the direction he is moving?
I am using a very simple script for movement and a fixed camera that's almost top down. I have searched all over for a way to make my character face the way he is moving but nothing seems to work with my script. Am I going to have to find another way to write the movements?
here is what I am using:
var speed:int = 10; var gravity = 10;
private var cc:CharacterController;
function Start(){
cc = GetComponent(CharacterController);
}
function Update(){
cc.Move(Vector3(Input.GetAxis("Horizontal")
* speed * Time.deltaTime, -gravity * Time.deltaTime,
Input.GetAxis("Vertical") * speed *
Time.deltaTime));
if (Input.GetAxis("Vertical") > 0.2)
animation.CrossFade ("run");
if (Input.GetAxis("Horizontal") > 0.2)
animation.CrossFade ("run");
if (Input.GetAxis("Vertical") < -0.2)
animation.CrossFade ("run");
if (Input.GetAxis("Horizontal") < -0.2)
animation.CrossFade ("run");
if ((Input.GetAxis("Horizontal") < 0.2)
&& (Input.GetAxis("Horizontal") >
-0.2) && (Input.GetAxis("Vertical") < 0.2) && (Input.GetAxis("Vertical") > -0.2))
animation.CrossFade ("Idle");
}
I applied some code formatting to make the question readable.
Answer by Graham-Dunnett · Aug 07, 2014 at 09:25 PM
The vector you pass to CharacterController.Move()
is the direction you want the character to move in. Just rotate the game object so it's forward vector is aligned with the direction you compute. The example on the Move() doc page shows a slightly more clean way to compute this vector.
Your answer
Follow this Question
Related Questions
What movment does the character do 1 Answer
How to Rotate the Player In Direction of Movement? 4 Answers
Making a character move automatically in one direction. 2 Answers
bullet problems 1 Answer