- Home /
Animations in character control
Hello, I am new here, so if I ask this in a wrong section, please tell me. I wrote a little script in js, for controlling my character and it works great. However, I want my character to play 'turning back animation' when we start to run / turn in opposite direction.
For example; We're holding right button and character runs. The running animation loops flawlessly. But when we push the opposite direction button, (left), character automatically start to runs the opposite. Technically it is okay, but I want to play a turning back animation between these runs. I have my turn back animation ready.
I hope I could explain the situation.
Script:
var isCollide:boolean=false;
function FixedUpdate () {
MoveHero();
JumpHero();
}
function PlayAnimation(AnimName : String) {
if (!animation.IsPlaying(AnimName))
animation.CrossFadeQueued(AnimName, 0.3, QueueMode.PlayNow);
}
function CheckForIdle() {
if (animation.IsPlaying("run")) PlayAnimation("idle");
if (!animation.isPlaying) animation.Play("idle");
}
function CheckForTurn() {
if (animation.IsPlaying("run")) PlayAnimation("turn");
if (!animation.isPlaying) animation.Play("turn");
}
function MoveHero() {
if (isCollide){
CheckForIdle();
} else {
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.2) {
if (Input.GetAxis("Horizontal") > 0.02) {
transform.eulerAngles.y = -90; //run left
} else {
if (Input.GetAxis("Horizontal") < -0.02) {
transform.eulerAngles.y = 90; //run right
}
}
transform.Translate(Vector3.forward * Mathf.Abs(Input.GetAxis("Horizontal")) * Time.deltaTime * 3);
if (!animation.IsPlaying("runningjump3")) PlayAnimation("run");
}
else CheckForIdle();
}
}
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
How can I tell Unity to wait to do something until after and animation has finished playing? 1 Answer
Using W,A,S & D to control character. 1 Answer
Character ragdoll to character animation? 2 Answers
How do i make a character accelerate and play an animation when the click left-shift 1 Answer