- Home /
Animation Scripting Help
I have wrote this script which is supposed to loop an animation when the LMB is held down and when let go stops the animation. But it only plays my animation once, im not sure what ive missed out can someone please help me?
function Start () {
animation["shoot"].speed = 2.5;
animation.wrapMode = WrapMode.Loop;
}
function Update () {
if ( Input.GetButtonDown("Fire1")) {
transform.animation.Play("shoot");
if ( Input.GetButtonUp("Fire1")) {
transform.animation.Stop("shoot");
}
}
}
Answer by SteveFSP · Nov 12, 2011 at 08:27 PM
It looks like the problem is that you are setting the wrap mode on the animation object rather than the clip's animation state.
The code where you you are setting the wrap mode should be as follows:
animation["shoot"].wrapMode = WrapMode.Loop;
Edit: There is a second, unrelated problem with the code. (It doesn't have an impact on your looping issue.) Here is the correct code to get the behavior you are looking for.
function Start () {
animation["shoot"].speed = 2.5;
animation["shoot"].wrapMode = WrapMode.Loop;
}
function Update () {
if (Input.GetButtonDown("Fire1")) {
transform.animation.Play("shoot");
}
// In the original code you have this inside the button down check,
// so the button up is never detected.
if (Input.GetButtonUp("Fire1")) {
transform.animation.Stop("shoot");
}
}
I have done this but it still dosent loop, is there anything I need to do outside the script?
If you want, you can go to the Asset, in the Inspector, and select Loop in the dropDown for "shoot" (the checkbox next to it is for smoothing -- can leave it off.) That does that same thing as the code line above.
I found a second issue with your code. (See edit.) I've also run the fixed code in my own project. The looping and start/stop works as expected. If you are still having problems, it doesn't look like it is the code.
Oh, and to answer your comment: $$anonymous$$y tests used a generic non-looping animation. No need to do any special setup. Just added the clip to the game object's animation component and attached the above script. Everything worked fine. At the moment, I can't think of any reason it isn't working for you.
if i wanted to transfer this to a walk animation so that when i press "w" would i just change Fire1 to w? cause when i tried changing Fire1 to w it didnt work
Your answer
Follow this Question
Related Questions
Loop Animation In Script? 2 Answers
Loop Animation Help 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Animation On Mouse Click 1 Answer
Aim down sights animation help 1 Answer