Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by the_motionblur · Nov 19, 2014 at 04:54 PM · visionattached-collider

Do child object triggers trigger the parent with a separate script?

I am working on my own version of the nightmares project at the moment and encountered an odd problem.

So I want to improve upon the existing zombunny enemy with a vision filed so that he only starts running towards the player if the player is in his field of vision. I'm trying to base my solution upon the CCTV cameras from the stealth project in a way that the enemy has a TriggerCollider in front of him to determine whether the enemy can actually see the player or not.

Since the Zombunny still has another sphere collider (trigger) as an attack range attached to the parent object I wanted to attach a secondary vision field trigger as a child object to the player.

So I have the main enemy object with scripts for navigation, attacking and health attached. Also with a sphere trigger very close to the enemy so that it can attack if the player is close to it. As a sub-object there's a second "visionCone" object with the enemyVision script and another Trigger Collider attached.

If the player enters the trigger the code triggers as expected:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyVision : MonoBehaviour {
 
     public Transform lookAtObject;
     public float visibilityRange = 8f;
 
     EnemyMovement enemyMove;
 
     Ray playerVisibility;
 
 
     void Start ()
     {
         enemyMove = GetComponentInParent<EnemyMovement>();
     }
 
     void OnTriggerStay (Collider other) {
         if (other.tag == "Player")
         {
             enemyMove.chasePlayer = true;
         }
     }
 }

... the enemy follows the player. So the trigger is working but the child object is apparently treated as a compound collider and also triggers the damage script on the main object.

Is there a way in which I can either prevent the viewRange Trigger from triggering the parent object scripts as well?

Or alternatively: Can I somehow restrict OnTrigger functions to a specific triggerzone on my GameObject?

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 Kiwasi · Nov 19, 2014 at 06:41 PM

Best way to solve is to put both triggers on seperate child objects. Put the appropriate scripts on each child.

Unity calls the various physics functions on the trigger and parent rigid body.

Comment
Add comment · Show 2 · 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 the_motionblur · Nov 19, 2014 at 06:45 PM 0
Share

Thanks. That worked. I guess it is by design that child triggers are called as compound triggers, then? $$anonymous$$eaning they can't be separated out that way by code?

avatar image Kiwasi · Nov 19, 2014 at 07:24 PM 1
Share

I believe they can be separated by code, but its more complex. Google may help here.

avatar image
2

Answer by Immanuel-Scholz · Nov 19, 2014 at 05:03 PM

Yes, you can restrict different groups of colliders to collide with each other.

The easiest way is to put one of the gameObjects (e.g. the vision collider gameObject) into an own Layer, e.g. "VisionLayer" (you have to create that layer first under Edit/Project Settings/Tags and Layers).

Then open Edit/Project Settings/Physics (Physics2D if you want to restrict 2D collider) and uncheck the checkbox between the default layer and the new VisionLayer.

Comment
Add comment · Show 3 · 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 the_motionblur · Nov 19, 2014 at 06:27 PM 0
Share

That's a good tip, thanks. Though it probably won't help in this setup, I'm afraid. If I set the trigger on the child object to not collide with the default layer then the script won't trigger the enemy chasing the player, either. I need the script to trigger as soon as the player enters the trigger zone - I just don't want the parent to be triggered by the child collider as well.

zombunny Setup

Both triggers need to be able to interact with the player but the parent script should not be triggered by the child trigger.

zombunnysetup.jpg (164.7 kB)
avatar image Immanuel-Scholz · Nov 20, 2014 at 07:48 AM 1
Share

I see that you already got a satisfactory answer. However, if you running into difficult collision setups with unity Physics again, here is another great function to keep in your tool belt: Physics.IgnoreCollision.

It works great if you are instantiating new colliders and know a small set of gameObjects like "the one player" or "these 3 laser gates in this level" where these colliders should just pass through - trigger and collision-wise.

avatar image the_motionblur · Nov 20, 2014 at 08:44 AM 0
Share

Cool- thanks a lot. I'll have a look at that for future enemy types ir if I'll dig into the object to replace more functions. :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

lighting/vision 0 Answers

How to make an objects texture what a camera is seeing 2 Answers

Enemy VISION range? Help! 0 Answers

Increase Field of vision of Enemy over time 1 Answer

OnTriggerEnter for a collider attached to another gameObject 2 Answers


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