- Home /
How do I make it so my attack animation in my swords animation controller only play when I press the mouse button.
I've tried adding an attack Boolean and setting it up with a behavior script, I've tried using blend trees, I've tried using sub-state machines. This driving me crazy as I've been doing this for the past 6 hours trying to figure it out. HELP!
So far I have this as my behavior script for the the starting state that transitions to the attack state.
using UnityEngine;
using System.Collections;
public class AttackAnimator : $$anonymous$$onoBehaviour {
private Animator myAnimator;
// Use this for initialization
void Start () {
myAnimator = GetComponent <Animator> ();
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown (0))
myAnimator.SetBool ("attack", true);
//Invoke ("StopAttack", 0.1f);
}
//void StopAttack () {
//animator.SetBool ("attack", false);
//}
}
Can you add a screenshot of how your animator controller is set up ?
Answer by Requiem36 · Jul 21, 2015 at 09:08 AM
From the code you've posted, you're only setting the bool to true once and never setting it to false. This will trigger the "attack" state indefinitely, so you have to set it back to false eventually. From what you have commented in your code, you were on the right track, thought I would advise using the actual animation time for the invoke delay.
I realize it will need to be set back. But for now I just wanted it to get working.
Answer by notoriousnary · Jul 21, 2015 at 07:22 PM
According to your code
if (Input.GetMouseButtonDown (0))
myAnimator.SetBool ("attack", true);
but your animator setup http://imgur.com/CmafYB7 is expecting a bool called fire which explains why the its not working. (based on the code you posted) set it to myAnimator.SetBool("Fire", true); and it should work.
That was actually previous code and my new stuff looks the exact same but with the right names. Still doesn't work.
Without loading your project up in unity, I don't think i can help much further. as long as the script is attached to the correct GameObject in the editor.
for reference, i'm using the same system in my project
Animator setup : http://imgur.com/Aztsgqn Script setting the flag: http://imgur.com/bI6eey2 Animator at runtime: http://imgur.com/DxftWVj
If you want to share your project in some way you feel comfortable I might be able to help work it out.
Answer by Ness · Jul 21, 2015 at 08:52 PM
There is some transition error displayed on the screenshot of you animator window, never came across it, fixing it would be my first step to debug this problem.
Furtheremore to debug this, I would setup game scene window next to animator window and observe behaviore of animator while clicking. Maybe your exit trigger is setup in a way that it exits to the previous animations just after entering it. Make a gif of animator window and post it, maybe this will tell more.
Your answer