- Home /
 
How to control a third person characters facing direction
Here's the link to the game I'm working on.
https://dl.dropbox.com/u/54318296/deez_world/WebPlayer/WebPlayer.html
Use arrow keys to move the player, space to jump, and a key to punch. I'm having trouble coding the character to face the direction he is moving in.
This is the code used.
 private var controller : CharacterController; 
 
 
 
 
 
 function Start ()
 
 
 
 {
 
     controller = GetComponent(CharacterController); 
 
     animation.wrapMode = WrapMode.Loop;
 
  
 
     animation["jump"].layer = 10;
 
     animation["jump"].wrapMode = WrapMode.ClampForever;
 
  
 
     animation["fall"].layer = 10;
 
     animation["fall"].wrapMode = WrapMode.ClampForever;
 
  
 
     animation["land"].layer = 10;
 
     animation["land"].wrapMode = WrapMode.Once;
 
     
 
 
 
     
 
     animation.Stop();
 
     animation.Play("idle");
 
 }
 
  
 
 function Update ()
 
 {
 
     var MovementY = controller.velocity.y;
 
     var controller : CharacterController =  GetComponent(CharacterController);
 
  
 
     if ((MovementY > 0.1) && !(controller.collisionFlags & CollisionFlags.Below))
 
     {
 
     animation.CrossFade("jump");
 
     }
 
     else if ((MovementY < -5) && !(controller.collisionFlags & CollisionFlags.Below))
 
     {
 
     animation.CrossFade("fall");
 
     }
 
  
 
     if ( ((animation.IsPlaying("jump")) || (animation.IsPlaying("fall"))) && (controller.collisionFlags & CollisionFlags.Below))
 
     animation.CrossFade("land");
 
  
 
     
 
     else
 
       animation.CrossFade("idle");
 
       
 
     
 
     if (Input.GetKey("down"))
 
         animation.Play("backrun");
 
     
 
      if (Input.GetKey("up"))
 
          animation.Play("run");    
 
         
 
     
 
     if (Input.GetKey("a"))
 
         animation.CrossFade ("punch");
 
         
 
               }
I having trouble making my player rotate properly and making the character stay facing towards direction of the last key pressed.
Any suggestions?
Answer by robertbu · Mar 25, 2013 at 03:19 AM
My experience with character controllers and animations is very limited, but conceptually this the code might solve your problem. Place it in Update().
 var lookPos = controller.velocity;
 lookPos.y = transform.position.y;
 if (lookPos.sqrMagnitude > 0.0) {
     lookPos += transform.position;
     transform.LookAt(lookPos);
 }
 
              Your answer
 
             Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Undesirable character flipping 0 Answers
Turn in the direction of movement 0 Answers
[HELP] Rotate Bullet Script 0 Answers
keeping the player upright 1 Answer