- Home /
walk animation script
i have a walk animation script that looks like this, but doesn't quite work properly can anyone tell me what is wrong with my code? and/or tell me how to fix this?
function Start () {
animation["walkanimation"].speed = 1;
animation["walkanimation"].wrapMode = WrapMode.Loop;
}
function Update () {
if (Input.GetButtonDown("w")) {
transform.animation.Play("walkanimation");
}
if (Input.GetButtonUp("w")) {
transform.animation.Stop("walkanimation");
}
}
Define "Doesn't quite work properly". What do you want it to do? How have you tried achieving this? Why do you think it doesn't work the way you want it to?
Answer by Rabbit · Nov 03, 2013 at 01:21 AM
I find it easier to use ENUM states to handle my animations, it makes it simple to add more animations later and also makes integrating multiplayer animation easier.
Declare this
public enum CharacterState
{
idle,
walking
}
public CharacterState _state;
public string idleAnimName;
public string walkAnimName;
void Update()
{
CheckKey();
}
void CheckKey()
{
if(Input.GetKeyDown(KeyCode.W)) {
_state = CharacterState.walking;
} else if (Input.GetKeyUp(KeyCode.W)) {
_state = CharacterState.idle;
}
PlayAnimation();
}
void PlayAnimation()
{
switch(_state)
{
case CharacterState.idle:
animation.CrossFade("idleanimation");
break;
case CharacterState.walking:
animation.CrossFade("walkanimation");
break;
}
}
Your answer
Follow this Question
Related Questions
FPS game walk animation script help 1 Answer
Animation Won't Start 3 Answers
Mecanim not updating script when it switches state? 2 Answers
Attack Animation through Javascript 0 Answers
Mixamo Animation: How to rotate my character while walking? 0 Answers