- Home /
How to play animation with one click
I'm trying to make a lightsaber game and it's looking pretty good but I've run into a problem. When I play the animation for my lightsaber attack (called "Attack" and "Attack2") it won't play the animation unless i hold down the mouse button. I want it to be to where i can click once and it plays the animation. I thinks the problem is i have a part in the script where it says if no button is being pushed, play the idle animation. How do I fix this problem? Here's my script:
var Rnd : int;
var anim : Animator;
function Update ()
{
if (Input.GetKeyDown(KeyCode.W))
{
anim.Play("Walk");
}
if (Input.GetKeyDown(KeyCode.A))
{
anim.Play("Walk");
}
if (Input.GetKeyDown(KeyCode.S))
{
anim.Play("Walk");
}
if (Input.GetKeyDown(KeyCode.D))
{
anim.Play("Walk");
}
if (Input.GetButtonDown ("Fire1"))
{
Attack();
}
if (Input.anyKey == false)
{
anim.Play("Idle");
}
}
function Start()
{
InvokeRepeating("Randomize",0,1);
}
function Randomize()
{
Rnd = Random.RandomRange(0,10);
}
function Attack()
{
if(Rnd < 5)
{
anim.Play("Attack");
}
else
{
anim.Play("Attack2");
}
}
Thanks in advance!
Answer by Hellium · Jun 19, 2015 at 10:07 AM
I think you have to check the state of your animator before calling the idle clip :
if (!anim.GetCurrentAnimatorStateInfo(0).IsName("Attack") && !anim.GetCurrentAnimatorStateInfo(0).IsName("Attack2"))
{
anim.Play("Idle");
}
Else, if you use the animator state machine, I think you can make a transition between your attacks animation clips to the Idle animation. Thus, you won't have to deal with it by scripting (same for walking)
This is awesome! The only problem is as soon as i it stops playing the attack animation, i want it to go back to the idle animation but ins$$anonymous$$d it doesn't do anything. And the walk animation has stopped working now. $$anonymous$$uch thanks though!
I think you should use the animator state machine for your problem.
Can you give a screenshot of your current animator please ? (Window > Animator)
Yep here it is:
Sorry I'm pretty new to Unity and I'm trying to figure out how the animator works. Up till now I've just used animations but I really want to learn how to use the Animator. Thanks for taking time to help!
First of all, I would recommend you to watch (maybe again) the Unity tutorials about animations :
http://unity3d.com/learn/tutorials/modules/beginner/animation/animator-component
I would have done this animator with three parameters :
Bool Walking
Trigger Attack
Trigger Attac2
Then, you use
public void SetBool(string name, bool value);
http://docs.unity3d.com/ScriptReference/Animator.SetBool.htmlpublic void SetTrigger(string name);
http://docs.unity3d.com/ScriptReference/Animator.SetTrigger.html
These two functions will be used as follow :
Set one of the two trigger when the mouse button is pressed with a random method like you did
Set the bool to true when W/A/S/D in pressed ($$anonymous$$eyDown)
Set the bool to false when W/A/S/D in released ($$anonymous$$eyUp)
BUT I am not used to work with animator, I can't guarantee this will work.
Considering @EwanAntranig 's use of a Random to vary the attack animation between two options, I would highly suggest he ins$$anonymous$$d use two Trigger
s, not one. Half the time, SetTrigger("Attack1")
, and the other half the time, SetTrigger("Attack2")
.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201141im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
clicking and attacking different animations 1 Answer
Check the current frame of an animation 2 Answers
Activating state upon mouse click in mecanim 1 Answer
picking up and using animated items/weapons. 0 Answers
Move while mouse button is held down 3 Answers