I need help writing a script that triggers a character animation when entering a collider
Basically, I am using mecanim as the source for my characters animations. I am not great with scripting but I figured out how to click move my character while activating the walking animation and then when not clicked she is in idle animation. My next step in the process is much harder for me. When she is near a certain object, like a box, I want another animation she has to be activated, like a reaction. Only when the character is near that object do i want that animation to play.
So far what I have done is connected the reaction animation "confused" to the idle animation in mecanim, created a bool parameter called "confused", and set the condition to activate the animation when true.I attached a collider to the box and clicked isTrigger on it. I thought it would be as simple as writing void OnTriggerEnter (Collider other) { anim.SetBool("confused", true); } however that is not the case and i looked at every tutorial, every question, every similar script and cannot figure it out. I would very much appreciate some guidance, thank you.
That is, essentially what you need to do, I would throw an if-statement in there to be extra safe that OnTriggerEnter is ONLY firing your SetBool, if the collided object happens to be your player, and the most common way people do this is through the tag.
OnTriggerEnter (Collider other){
if( other.gameObject.tag == "Player"){ anim.SetBool("confused",true); } }
BUT, you need to have the Animator component on your player, as well as this script -- now if you have this script on your player, then you dont want to do == "Player", you want it to be whatever object your colliding with's tag... Vice Versa if the script is ON the object your colliding with, then you DO want it to be == "Player". BUT if it is on the object your colliding with, anim.SetBool wont work, you need to get the players anim.SetBool - which a little bit of a longer path: other.gameObject.GetComponent().SetBool... Assu$$anonymous$$g that the player itself, top higherachy collided - and by that I mean, you could have a parent called your player, in that, could be the break down of every mesh, bone, etc of your character, and what actually collides with the object first, could be, for example Left Foot Bone 9... Ins$$anonymous$$d of your entire character as a whole, if your character is not all 1 mesh with no parent/child hierarchy structure. This could be why nothing appears to be happening as well.
One way you could get around that, is do another if-check:
OnTriggerEnter (Collider other){
if( other.gameObject.tag == "Player"){ if(other.gameObject.transform.parent == null){ //you know that what actually collided, WAS the parent top-most hierarchy of your player other.gameObject.GetComponent().SetBool("confused",true); } else { //you know that what actually collided, was a CHILD of your parent top-most hierarchy, so you need to climb to your parent other.gameObject.transform.parent.parent.parent .GetComponent().SetBool("confused",true); } } }
And im sure there is an easier way, you could even try a very specific GameObject.Find(); if you dont have too many objects in your scene, or try a direct public GameObject linkage and try messing with that, but it all depends where the script is attached to, and what it actually has direct access to, plus what it is ACTUALLY colliding with at the time.
At worst case scenario, if you cannot figure it out, your welcome to email me with some visuals, and I can try my best to guide you through the solution, the problem could also lye in how you actually have your animation setup as well. (you can find my email on my profile)
That made so much sense! thank you, my problem is that i was attaching the script to the object but it is way easier attaching the script to the character and writing the if statement for the objects tag. It works exactly the way I wanted to.