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
3
Question by Oninji · Nov 29, 2010 at 10:57 PM · collidertrigger

Other function that is similar to OnTriggerStay

Hello,

I need something to detect like "OnTriggerStay" does, but that I could call whenever I want.

I need to detect objects in a specific zone, but not every frame because it consume too much CPU.

Any Ideas?

Thank You.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Dec 22, 2010 at 06:56 PM

Try this code on. It will call the method < callbackName > with a callback of your choosing. framesToSkip is the number of frames that should be skipped until the next callback is raised.

public class TriggerFrameSkip : MonoBehaviour { public string callbackName = "OnTriggerFrameSkip"; public int framesToSkip = 1;

 int elapsedFrames;    
 List&lt;Collider&gt; colliders = new List&lt;Collider&gt;();

 void Update()
 {
     if (elapsedFrames++ &lt; framesToSkip) return;
     elapsedFrames = 0;
     RaiseCallback();
 }

 void RaiseCallback()
 {
     foreach (var collider in colliders)
         SendMessage(callbackName, collider, 
             SendMessageOptions.DontRequireReceiver);
 }

 void OnTriggerEnter(Collider collider)
 {
     colliders.Add(collider);
 }

 void OnTriggerExit(Collider collider)
 {
     colliders.Remove(collider);
 }

}

Or if you'd rather have it time based:

public class TriggerTimeSkip : MonoBehaviour { public string callbackName = "OnTriggerTimeSkip"; public float interval = 5.0f;

 List&lt;Collider&gt; colliders = new List&lt;Collider&gt;();

 void Start()
 {
     InvokeRepeating("RaiseCallback", float.Epsilon, interval);
 }

 void RaiseCallback()
 {
     foreach (var collider in colliders)
         SendMessage(callbackName, collider, 
             SendMessageOptions.DontRequireReceiver);
 }

 void OnTriggerEnter(Collider collider)
 {
     colliders.Add(collider);
 }

 void OnTriggerExit(Collider collider)
 {
     colliders.Remove(collider);
 }

}

Then your others scripts can be implemented such as:

function OnTriggerTimeSkip(collider : Collider)
{
    // called like OnTriggerStay but only every x seconds.
}

Note that you can rename the callback you want to use. Don't use OnTriggerStay, OnTriggerEnter or OnTriggerExit since the latter two will produce errors and the first will cause unwanted behaviour.

In case you want to control when to send the event yourself, you could use this base class:

public class TriggerBase : MonoBehaviour { List<Collider> colliders = new List<Collider>();

 protected sealed void RaiseCallback(string callbackName)
 {
     foreach (var collider in colliders)
         SendMessage(callbackName, collider, 
             SendMessageOptions.DontRequireReceiver);
 }

 protected virtual void OnTriggerEnter(Collider collider)
 {
     colliders.Add(collider);
 }

 protected virtual void OnTriggerExit(Collider collider)
 {
     colliders.Remove(collider);
 }

}

I think you need to override OnTriggerEnter and OnTriggerExit to call base.OnTriggerEnter and base.OnTriggerExit in your script. I am too lazy to check this now. I marked RaiseCallback sealed because there isn't really any intention for you to change its behaviour. Your code should instead just call RaiseCallback("OnTriggerWhatever"); whenever you find it fitting.

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 Statement · Dec 22, 2010 at 07:04 PM 0
Share

Caveat: $$anonymous$$ake sure you clean out colliders of any null values if objects are deleted while in the area. I am not certain if they send OnTriggerExit before they are destroyed.

avatar image Statement · Dec 22, 2010 at 07:16 PM 0
Share

Another Caveat: This will cause your callbacks to arrive in another order than the usual "triggers first-update later". Now you can't tell which will come first. It probably doesn't do you any harm though, it all depends on what you want to do in the callback.

avatar image
0

Answer by Jan Helleman · Nov 30, 2010 at 01:02 AM

you could turn it around and make the zone a trigger and not the objects

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 Oninji · Nov 30, 2010 at 01:29 AM 0
Share

The zone is a Trigger, which check the presence of objects within if they meet some condition. The problem, OnTriggerStay is called every frame, which is very CPU intensive. So I need the exact same functionality, but called every 5 second or so.

avatar image
0

Answer by BitMiller · Dec 22, 2010 at 05:51 PM

You could assign it to a variable and turn the gameObject having the trigger on once every 5 seconds.

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

No one has followed this question yet.

Related Questions

Using a trigger collider to detect enemies. 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Animation not playing but trigger works 1 Answer

Have something be a trigger and a collider? 1 Answer

Script error: OnTriggerEnter 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