Animation Script TPS game
Hi everybody ! I try to make a script for the animations of my character, where is the error ? Help me ^^"
#pragma strict
function Start () {
print ("Trying to animate that.");
}
function Update() {
if (Input.GetKeyDown(KeyCode.W)) {
GetComponent.<Animation>().CrossFade("walk_inPlace");
} else if (Input.GetKeyDown(KeyCode.A)) {
GetComponent.<Animation>().CrossFade("run_left");
} else if (Input.GetKeyDown(KeyCode.D)) {
GetComponent.<Animation>().CrossFade("run_right");
} else if (Input.GetKeyDown(KeyCode.S)) {
GetComponent.<Animation>().CrossFade("run_backward");
} else {
GetComponent.<Animation>().CrossFade("idle");
}
}
Thanks !
i dont know much about animation but the method you are using there seems very inefficient, to get a component every time can't be good.
http://docs.unity3d.com/ScriptReference/Animation.CrossFade.html
maybe this will help
Answer by Statement · Oct 11, 2015 at 03:54 PM
Try using Input.GetKey instead of Input.GetKeyDown. GetKeyDown only return true the frame the key was first pressed. The next frame it will return false even if the key is held down.
#pragma strict
private var anim : Animation;
function Start () {
anim = GetComponent.<Animation>();
}
function Update() {
if (Input.GetKey(KeyCode.W))
anim.CrossFade("walk_inPlace");
else if (Input.GetKey(KeyCode.A))
anim.CrossFade("run_left");
else if (Input.GetKey(KeyCode.D))
anim.CrossFade("run_right");
else if (Input.GetKey(KeyCode.S))
anim.CrossFade("run_backward");
else
anim.CrossFade("idle");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612040552im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Disabling object in animation 0 Answers
animations not working on multi play mode? 0 Answers
Different weapon animations 1 Answer
How to export a finished game? (Help please) 4 Answers
Help with jump script 1 Answer