Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by jmarti9852 · Nov 16, 2016 at 07:26 AM · animationscript.triggers

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dibbie · Nov 16, 2016 at 09:38 AM 1
Share

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)

avatar image jmarti9852 Dibbie · Nov 16, 2016 at 04:54 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

148 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I trigger two animation 2 Answers

Simple 2D movement with animation while you are moving 0 Answers

Trigger the Animation #2 after Animation #1 was triggered 0 Answers

Help me pLease (important) pls reply soon.. 0 Answers

[2D] Play animation backward. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges