- Home /
How to have a collider inactive only during attack animation and not during 'charging' animation?
When the player holds mouse 1, I want a charging animation to play once, and the attack animation to then loop. I have the animations themselves down, and a collider is activated upon attacking, however the collider is also present while charging, as the charging animation leads into the attack animation. How do I make it so that the collider is only active during the attack animation.
public class Hand : MonoBehaviour {
private Animator anim;
public CapsuleCollider cc;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
cc.enabled = false;
}
// Update is called once per frame
void Update () {
if (Input.GetButton("Fire1"))
{
anim.SetBool("IsNoInput", false);
anim.SetBool("IsCharging", true);
anim.SetBool("IsAttacking", false);
}
else
{
anim.SetBool("IsNoInput", true);
anim.SetBool("IsCharging", false);
anim.SetBool("IsAttacking", false);
}
if (anim.GetBool("IsAttacking") == true)
{
cc.enabled = true;
}
if (anim.GetBool("IsAttacking") == false)
{
cc.enabled = false;
}
}
public void ActiveCollider(int active)
{
if (active == 0)
cc.enabled = false;
else
cc.enabled = true;
}
}
This current code has the charging animation play, but it stops at the last frame and doesn't move on to the attack animation. If it helps at all the charging animation is .708 seconds long.
Answer by cstooch · Jun 23, 2017 at 11:48 PM
I wonder if animation events are your best bet here.
You basically would have function(s) to disable/enable the collider. In your animation for charging you set up an event to call the function for disabling. In your animation for attacking, you set up an event to call the function for enabling.
That may work.
https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html
These events can also be good for switching animations too, like you could put an event right at the end of charging animation that turns the charging animation parameter off and the attack animation on (if mouse is held down, I think you'd want to put)
I've considered animation events. I'm fairly new to unity, and while I know where to find them and can put the events at the correct places, I'm unsure what to put for function to have it do anything.
That isn't too tough to explain, no worries. I was actually brand new to them as well, up until last week when I needed to figure out something for my game.
So, basically your animation event can call any function that is attached to a script on the game object your animation is attached to.
So you really already have everything set up for the collider part as far as I can tell. You'll need to remove some of the other code you're doing because that will just be confusing things. But basically your event will call the function ActiveCollider , and you set int value to 1 or 0 (it passes as parameter), and for "Object" in the animation event, you put your script that this function is in... (Hand??).You'd also have to create a function that just simply sets your various animation parameters.. like a function to set the parameters so that your state for the charge is called, and a function to set the parameters so that your state for the attack is called.
I hope that gets you started.
That link I sent actually explained how to add an animation event in a way I thought was a little less easier to understand though. I know you can double click on the state in the animator, and it will bring up your animation in the inspector... and there's an Events section where it seemed a lot more intuitive...
It's right below where you have all those checkboxes for Loop time, loop pose, baking rotation into pose, etc...
Thanks for the help! I got it working! All I really had to do was set an animation event at the beginning of the attack animation to turn the collider on and an event at the start of the idle animation to turn it off.
Awesome!
Now you're an animation event pro! lol
Your answer
Follow this Question
Related Questions
collider animation problem 1 Answer
Character Animator controller/movement 1 Answer
Is there a new retargetting system for the animation in 5.5? 1 Answer
How to rotate spine by the script, while an animation from the animator is playing? 0 Answers
How do you allow the joints in a character to freely fall based on gravity? 1 Answer