- Home /
How do I fix this broken walking animation start in C#?
Hello all, Here is an extremely basic C# script that I wrote to test out a walking animation. I will go back later in the project and hopefully add booleans to control it so I can add jumping animations and the like. I have the walking animation all done. It is attached to the pistol model, and to make sure I dragged the animation to the pistol as well. I then dragged this script to it. I click play and it just plays the animation over and over no matter what I do. I just want some light shed on what I am doing wrong. Thanks.
using UnityEngine;
using System.Collections;
public class walkinanimationpistol : MonoBehaviour {
void Update () {
if (Input.GetKeyDown ("w")) {
animation.Play("NameOfWalkingAnim");
} else {
animation.Stop("NameOfWalkingAnim");
}
}
}
from that code i cant see a problem. But you wrote, that you dragged the animation onto the pistol. Is that intended? I would think that it had to be on the character. Because if you reference the character animation component with your "animation", but the actual clip is not in its list, it will only play (if ticked "play automatically") the default animation.
Answer by sath · Jan 09, 2014 at 07:45 AM
you are in Update so pushing "w" start animation but in a frame because your are stopping the animation with if else so make it like this
public class walkinanimationpistol : MonoBehaviour {
void Update () {
if (Input.GetKeyDown ("w")) {
// animation.Play("NameOfWalkingAnim");
print("play");
} else if (Input.GetKeyDown ("s")){
// animation.Stop("NameOfWalkingAnim");
print("stop");
}
}
}
but in his case the animation was looping even though it should have instantly stopped. If it wasn't the problem this would be his corrected code:
if (Input.Get$$anonymous$$ey("w")){
//play
}else{
//stop
}