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
1
Question by MachCUBED · Apr 06, 2012 at 12:32 AM · collisioncollidercollisiondetection

How do I make OnTriggerEnter() respond to just one collider?

Hi guys,

I have some checkpoints in my game, and I've decided to use OnTriggerEnter() for them. Here is the implementation in Checkpoint.js:

function OnTriggerEnter (other : Collider) {

 if (other == engine.collider);
 levelManager.SendMessage("RegisterCheckpointHit");

}

levelManager is set to a shared instance of the prefab LevelStateManager, and it has a script attached called LevelStatus.js. The implementation of RegisterCheckpointHit is as follows:


function RegisterCheckpointHit()
{
if (checkpointsHit < numCheckpoints) checkpointsHit++;
Debug.Log("Checkpoint Hit");
}

The RegisterCheckpointHit() function works exactly as expected, however OnTriggerEnter gets called multiple times because the player object has multiple colliders. The player object consists of a character with multiple colliders, as well as an engine with just one collider. Because there are multiple colliders, the collider for the checkpoint gets triggered multiple times. The attempt to restrict it to the engine's collider with the following code:


if (other == engine.collider);

Doesn't seem to be working. What am I doing wrong?

MachCUBED

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

Answer by Kryptos · Apr 06, 2012 at 08:16 AM

Take a look at Layer-Based Collision Detection.

What you can do is having a child object on your player with a collider that is in the only layer that collides with the layer of your triggers.

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 MachCUBED · Apr 06, 2012 at 05:55 PM 0
Share

Putting my checkpoints on their own layer, then setting the Checkpoints layer to only interact with the Jetpack layer didn't do the job either, OnTriggerEnter is still being triggered twice by the sole collider of the Jetpack. This is with the code in LevelStatus.js set to the following:

function RegisterCheckpointHit() { if (checkpointsHit < numCheckpoints) { checkpointsHit++; }

if (checkpointsHit > numCheckpoints) { checkpointsHit = numCheckpoints; }

if (checkpointsHit == numCheckpoints) { NotificationCenter.DefaultCenter().PostNotification(this, "ActivateFinishLoop"); }

Debug.Log("Checkpoint Hit"); }

avatar image voporak5 · Dec 04, 2013 at 11:13 PM 0
Share

wow, so stupid that ontriggerenter gets called for the parent object and all children with colliders of the parent object. been confused for months as to why my guy was losing 2 lives after being killed!

avatar image
0

Answer by simonmc · Apr 06, 2012 at 12:35 AM

remove the semicolon in your if (other == engine.collider) line

to avoid all sorts of errors with if's in future, make use of curly braces for if statements.

 if (other == engine.collider) {
       levelManager.SendMessage("RegisterCheckpointHit");
 }
Comment
Add comment · Show 1 · 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 MachCUBED · Apr 06, 2012 at 03:08 AM 0
Share

When I tried the above code, I got a thing where the checkpoint gets activated twice for some strange reason.

I also added a boolean to deter$$anonymous$$e if the checkpoint has been hit or not.


private var passed = false;

And the new contents of OnTriggerEnter():


    if (other == engine.collider) 
    {
        if (!passed) 
        {
            Debug.Log("Registering Checkpoint Notification");
            NotificationCenter.DefaultCenter().PostNotification(this, "RegisterCheckpointHit");
            passed = true;
        }
    }

Your code helped, but I still have a strange issue nonetheless. Although the notification is only being posted once, the rest of the dependent code is being run twice, even though RegisterCheckpointHit() is implemented in two separate files with different implementations.

LevelStatus.js:


function RegisterCheckpointHit()
{
if (checkpointsHit < numCheckpoints) checkpointsHit++;
Debug.Log("Checkpoint Hit");
}

HUD.js


function RegisterCheckpointHit ()
{
    timeLeft += checkpointBonus;
    Debug.Log("TI$$anonymous$$E EXTENDED");
}

Strange, huh?

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

6 People are following this question.

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

Related Questions

Non - Physics collision System 0 Answers

In-game pickup not moving towards player after being collected 1 Answer

How do I get a Not collision? 5 Answers

How to make collision check if other collider has component 1 Answer

Find colliders after collision? 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