- Home /
Animator not playing onCollisionEnter.
Hey, I want ammo box to open and close when Player collides with it. My code looks like this: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AmmoBoxAnimator : MonoBehaviour
{
Animator animator;
private void Start()
{
animator = GetComponent<Animator>();
}
private void OnCollisionEnter(Collision col)
{
if(col.transform.tag == "Player")
{
animator.Play("opened_closed");
}
}
}
I attached my script to every ammo box I have and checked the name of animation:
Why isn't it working? Everything is fine I guess...
Answer by Otavio_ · Dec 26, 2018 at 11:16 PM
Create another state in animation, empty, set as default;
Make a transition from him to your animation, and on this transition, put a bool condition and select "true", also uncheck "has exit time";
Change this part of your script:
if(col.transform.tag == "Player") { animator.Play("opened_closed"); }
to
if(col.transform.tag == "Player")
{
animator.SetBool("nameofyourcondition", true);
}
and this should work...
Hint: i suggest to you to change the "collider checker" from every bullet to only the player. So, you will have less scripts checking every time if someone collide*
I've never used animations. The animation is from asset store. I will try this later though, and see if it works.
So I did what you told me, I guess I did it correctly :/ It's still not working though.
Here are screenshots links because for some reason I can't upload them from my PC: https://imgur.com/a/mc7Hwrh
you forget something: you have to create a parameter in animator > parameters > + > bool > set the name as you choose (playAnimation) then you go in the arrow of transition and re-choose the parameter that you just created
Okay, I've created a paramater "playAnimation" as you said should I set it to true or false in the transition? I've set it to false and It's not working.
Edit: true is working but only once and I want to it do the animation every time I collide with an object. How to do it?
Answer by Aspect13 · Dec 26, 2018 at 08:38 PM
Please someone help? Should I set the Animator to public at attach the animation to it?
Answer by RustyCrow · Dec 29, 2018 at 06:19 PM
You need to google unity animations my dude, i see that you have some basic understanding but you are missing the other half and the best way is to just look at some one else do it. But from what i see is you are missing 3 things Parameter A transition and a State(empty) (yours is default so it will happen on play) and Code to trigger (take a look at the Setbool() etc funcs). Going through every step needed i dont think anyone is willing to write down.
Thanks I did some research about what you said and it seems to be working the only problem is it plays only one time. $$anonymous$$g I collide with first ammo box it play animation, then I go to the second ammo box on the map and it plays animation etc. but when I go the first ammo box once again it does not play animation any more.