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 Rick74 · Aug 19, 2013 at 06:37 PM · javascriptontriggerenterontriggerstay

OnTriggerStay explanation?

I have created a tower defense style system, and for the most part, it works great. When objects enter the collisionbox of the turret, the turret attacks them until they. When they die, the turret stops firing.

I have noticed that there is some weirdness however, when 2 ore more enemies are in the collision box of the turret. If one dies, the turret fails to pick up on the other. And just stops firing all together.

I want to solve this with OnTriggerStay, but when I replace that function with my current OnTriggerEnter, the turret will not fire. If I just include an added OnTriggerStay function to work with my current OnTrigger Enter/Exit functions, again...the turret refuses to fire or detect enemies in it's collision box. So I'm clearly mis understanding how to use this function.

Here's my turret code;

 var rangeX : float = 0.0;
 var rangeY : float = 0.0;
 var rangeZ : float = 0.0;
 
 var projectile :         GameObject;
 var projectileSocket :     Transform; 
 var myTarget :             Transform;
 var reloadTime :        float     = 1.0;
 var nextFireTime :         float     = 0.0;
 
 
 function Update ()
 {
     if ( myTarget )
     {
         if ( Time.time >= nextFireTime )
         {
             FireProjectile ();
         } 
     } 
 }
 
 function OnTriggerEnter ( other : Collider )
 {
     if ( other.gameObject.tag == "alien" )
     {
         nextFireTime = Time.time + ( reloadTime * .5 );
         myTarget = other.gameObject.transform;
     }
 } 
 
 
 function OnTriggerExit ( other : Collider )
 {
     if ( other.gameObject.transform == myTarget )
     {
         myTarget = null;
     }
 }
 
 function FireProjectile ()
 {
     audio.Play ();
     nextFireTime = Time.time + reloadTime;    
     Instantiate(projectile, projectileSocket.position, projectileSocket.rotation);
 }
 
 function OnDrawGizmos ()
 {
     Gizmos.color = Color.white;
     Gizmos.DrawWireCube ( transform.position, Vector3 ( rangeX, rangeY, rangeZ ) );
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by OP_toss · Aug 19, 2013 at 07:29 PM

I would keep using Enter and Exit. Then just keep a Queue of your targets, Enqueuing whenever one Enters, and Dequeueing when one Exits. Then when one dies, Dequeue, and move to the next target, if it exists. Perfect use for a Queue.

Hope this helps!

Comment
Add comment · Show 10 · 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 Rick74 · Aug 19, 2013 at 08:37 PM 0
Share

I'm not familiar with Queue, could you give me an example of how it's used? (I'm relatively new to Java)

avatar image OP_toss · Aug 19, 2013 at 11:19 PM 0
Share

It's just like a queue in real life. First-in, First-out. People join the line at the end, the person at the front gets their meal, and gets dequeued off the front of the queue. $$anonymous$$ake sense?

The actual API for Queue can be found in the documentation online. All you need to use is Enqueue (push/add), and Dequeue (pop/remove), and maybe Clear.

avatar image Rick74 · Aug 20, 2013 at 04:28 AM 0
Share

I've done a search within Unity's API and nothing comes up to what I would need. It looks to me like allot of animation functionality, but not a first in/ first out style type function.

Could you point me in the right direction or just give me a small sample code of how it works wihtin Unity?

avatar image OP_toss · Aug 20, 2013 at 06:03 PM 0
Share

http://msdn.microsoft.com/en-us/library/7977ey2c.aspx

It's not a Unity thing. It's a .NET C# thing.

avatar image Rick74 · Aug 21, 2013 at 09:06 PM 1
Share

Being new to coding in general, just reading a description of Queue isn't enough for me to figure out how to properly understand or use it. I'm just not there yet. I need something more then just the API. I've been going through as many online tutorials as I can get my hands on, but so far none of them have touched on queue.

Thankfully OnTriggerStay is perfect for my situation and all I need right now. I'll keep digging into queue though, for future use, as what you described does sound like it would be helpful down the road.

Thanks again!

Show more comments

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

17 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

Related Questions

Trying to make the enemy fire 1 Answer

How to use OnTriggerEnter with multiple triggered objects? 1 Answer

How to make multiple gameobjects instantiate from one collider? 3 Answers

issues with on trigger enter and exit 1 Answer

Can I access a script OnTrigger WITHOUT using getcomponent? 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