- Home /
Check if FPS Character Controller Is Moving Then Switch Animations
I have one animation for when the character is moving and for when he is not. Im using the FPS Character Controller, so how to I check if its moving and then fade between two animations? Thankyou
EDIT: I am using the default FPS CC script as well.
Answer by aldonaletto · Dec 04, 2012 at 06:29 PM
The best way to add animation to the FPS prefab is to attach a simple script to it that measures its displacement each frame, calculates its velocity and fire the right animation - like this:
var walkSpeed: float = 0.2; // start walk animation above this speed
private var lastPos: Vector3;
function Update(){
var dist = Vector3.Distance(transform.position, lastPos);
lastPos = transform.position;
if (Time.deltaTime > 0){ // avoid errors when game paused
var speed = dist / Time.deltaTime; // calculate speed
if (speed > walkSpeed){
animation.CrossFade("walk");
} else {
animation.CrossFade("idle");
}
}
}
The same logic can be used to add a run animation, for instance:
var walkSpeed: float = 0.2; // start walk animation above this speed
var runSpeed: float = 3.0; // start run animation above this speed
private var lastPos: Vector3;
function Update(){
var dist = Vector3.Distance(transform.position, lastPos);
lastPos = transform.position;
if (Time.deltaTime > 0){ // avoid errors when game paused
var speed = dist / Time.deltaTime; // calculate speed
if (speed > runSpeed){
animation.CrossFade("run");
} else if (speed > walkSpeed){
animation.CrossFade("walk");
} else {
animation.CrossFade("idle");
}
}
}
Thankyou so much! Is there a way for it not to track movement on the Y axis?
Answer by eweeparker · Aug 03, 2014 at 09:44 PM
if(Mathf.Abs(player.GetComponent(CharacterController).velocity.x) > 1
Your answer
Follow this Question
Related Questions
Get the sin of an angle between vector3.forward and direction character is moving 1 Answer
Fixed Camera, Mouse aimer, How do I animate this? 1 Answer
Rigidbody velocity randomly returning zero? [Possible unity bug] 1 Answer
2D Animation Effects Velocity Wrong? 0 Answers
Increase NavMesh Rotation Speed 3 Answers