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 Gatau · Jun 26, 2016 at 02:43 PM · c#ontriggerstay

OnTriggerStay2D when trigger is spawned on object not moving

OnTriggerStay2D does not trigger if it's being spawned on an object which is not moving. If that object moves after the trigger has spawned it will activate the trigger.

How can I make this trigger fire on Start() if there are any enemies in it?

In my case it's an AOE field on the ground which should damage object staying inside it:

   void OnTriggerStay2D(Collider2D other)
   {
     float timerComparison = Time.time;
 
     if ((timerComparison - timer) * 1000 > 250)
     {
       other.gameObject.GetComponent<EnemyStats>().Damage(dpsValue / 4);
       timer = Time.time;
     }
   }

I think a coroutine always looking for objects inside this field would be more expensive and not very good for me as my scene could have quite a few if these?

Comment
Add comment · Show 3
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 phxvyper · Jun 26, 2016 at 02:55 PM 1
Share

Unity3D has a profiler built in. You should create an implementation in the Update() function and in a Coroutine to see which is more expensive on your CPU.

If youre not totally familiar with how the Profiler works, go here: https://docs.unity3d.com/$$anonymous$$anual/ProfilerWindow.html

That page and the pages that follow go in depth about how the profiling window works.

avatar image Gatau phxvyper · Jun 26, 2016 at 02:59 PM 0
Share

thanks, thats useful

avatar image phxvyper Gatau · Jun 26, 2016 at 03:02 PM 0
Share

You're welcome my dude. Always refer to the profiler first for any general profiling questions. If its not too obvious from the profiler, then ask away! Just make sure you checked the profiler first and make note of that in your edit/question.

1 Reply

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

Answer by Gatau · Jun 26, 2016 at 03:20 PM

The solution to my problem was inside the Rigidbody2D's Sleeping Mode, of the object which contains the above script, which had to be set to Never Sleep. The problem was not only related to when this trigger was spawned on an already existing object, but also when objects entered and then stopped moving inside this field.

The reason for this is explained on this page under the Sleeping section: http://docs.unity3d.com/Manual/RigidbodiesOverview.html

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 phxvyper · Jun 26, 2016 at 04:23 PM 0
Share

Don't forget to click the Correct Answer button below the Upvote/Downvote buttons to ensure this question is closed properly! :D

avatar image dengbunthaistudy phxvyper · Sep 13, 2020 at 07:43 AM 0
Share

it actually works:D

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

173 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

Related Questions

Move Player out of trigger 0 Answers

On-click and trigger animation c# 1 Answer

Moving grabbed object OnTriggerStay2D 0 Answers

Issues trying to activate an image through OnTriggerEnter/Exit/Stay(Flickering) 1 Answer

¿How can i add a fixed joint configured by script? 0 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