- Home /
Question by
Vylse · Feb 22, 2012 at 07:10 AM ·
charactercontrollerlookat
my char is facing downwards
im using LookAt to make my char face the direction he's moving, but the problem is he is facing downwards, here is my code:
var runspeed: float = 3;
var gravity: float =15;
var jumpSpeed:float = 5;
private var moveDirection: Vector3 = Vector3.zero;
static var grounded: boolean = false;
private var controller:CharacterController;
private var flags : CollisionFlags;
function Start () {
}
function Update () {
if(grounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
moveDirection *= runspeed;
if(Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y-=gravity*Time.deltaTime; //this what makes my char face downward
controller = GetComponent(CharacterController);
flags = controller.Move(moveDirection*Time.deltaTime*runspeed);
grounded = (flags & CollisionFlags.CollidedBelow) !=0;
gameObject.transform.LookAt(gameObject.transform.position + moveDirection);
}
@script RequireComponent(CharacterController)
im not good at using euler angles, so i used LookAt. how do i make him not to face downward w/out taking the variable gravity.
Comment
Answer by Berenger · Feb 22, 2012 at 07:27 AM
If moveDirection is a vector null, LookAt will probably behave like that. If the sqrMagnitude is < small value, you should not rotate.