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 SirOrange · Feb 11, 2017 at 10:22 PM · animationcollisioncollideranimatortag

How to make OnTriggerEnter Only work for certain Gameobjects?

How do I only make a cooking collider cook the chicken patties? I have already made a custom tag for the cook able items. Also need help to only have animation play when inside the trigger. using System.Collections; using System.Collections.Generic; using UnityEngine; public class CookFood : MonoBehaviour { %|1603254598_1|% %|1671281540_2|% %|736461570_3|% %|548796267_4|% %|284888560_5|% %|1596870978_6|% %|819998687_7|% %|-1699521485_8|% %|-1207445934_9|% void Update () { %|-1057342426_11|% %|-1731482512_12|% %|-420389490_13|% %|221049047_14|% %|-522694938_15|% %|-198492382_16|% %|-527023563_17|% }

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by T_Lavell · Feb 12, 2017 at 10:15 PM

FindWithTag is not to be confused with CompareTag.

OnTriggerEnter will be called whenever the gameObject your script is attached to comes into contact with any trigger - not necessarily just your stove object (unless it is the only trigger that exists). The reference you call 'Stove' is actually a reference to whichever other collider(trigger) came into contact with this one. So, if you want to trigger a 'Cook' animation when the player (or whatever the script is attached to) comes into contact with a Stove, or other object with the 'cook' tag, you need to perform a little discernment.

Try

 void OnTriggerEnter(Collider otherObject)
 {
     if (otherObject.gameObject.CompareTag("Cook") )
     {
         Animator _anim = otherObject.GetComponent<Animator>();
         _anim.SetTrigger("Cook");
         // Or you can use legacy Animation and Anim.Play code here instead.
     }
 }

What I've done is to first check if the trigger object has the tag "Cook", and then if it does, told its animator component to play the cook animation (the trigger "Cook" would have to be set up already in the AnimatorController as a parameter, and a transition set up). If you are using legacy animation, you could do so here - but you will want to be careful that you are playing the animation on the correct gameobject, etc. I wrote code that assumed the triggering object should play the animation - but you could instead piggyback a reference if you need.

'FindObjectWithTag' could reference any object in the scene with that tag, and returns true if it finds any such object.

Comment
Add comment · Share
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
0

Answer by SirOrange · Feb 11, 2017 at 10:23 PM

oops here'`using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CookFood : MonoBehaviour {

 public Collider Stove;
 public GameObject Chicken;
 public Animation Cook;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     
 }

 void OnTriggerEnter (Collider Stove){
     if(GameObject.FindWithTag("Cook")){
     Cook.Play ();
     }
 }

} `s the code

Comment
Add comment · Share
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

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

198 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 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

I want to trigger an animation upon collision with a 2D obstacle in my infinite runner game.Help me. 0 Answers

Play animation on collision 1 Answer

Opening a door with OnCollisionStay 2 Answers

How to trigger animation from object colliding with another? 0 Answers

Precise Hitboxes for third person game..animating mesh colliders..?,How to create precise HITBOX for a third person game that takes care of combat aswell as locomotion flawlessly? 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