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 wknight92 · May 16, 2012 at 06:55 PM · 2dontriggerenteralgorithm

store unique variable on first OnTriggerEnter

This is a 2D game:

 ||
 ||                `BC`
 ||             `Big  Cirle`
 ||     <--- `Big      Circle`
 ||              Big Circle`
 ||                `BC`
 ||
 ||
 ||

So Imagine the 'stick' on the left is a network of 9 cubes all stuck together. The 'Big Circle' is a sphere collider. Essentially the big sphere collider is the range of an auto-gun. When the cubes are in range I want the auto-gun to take out the first one that comes in range and that one only.

I also want it to take about a second, which I think can be done with a simple yield statement. After that second I want the autogun to start firing on the next closest cube(not exactly sure which one that is yet)...maybe they're already is an Algorithm for this.

Comment
Add comment · Show 5
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 hijinxbassist · May 16, 2012 at 07:43 PM 1
Share

@wknight92

In the trigger function you can get the first cube and store it in a var, but also wrap that part of the trigger code with a

 if(enemyCube==null)enemyCube=collision.gameObject;

this way you will only fire at the one cube until it is destroyed(becomes null), then it will store the next cube and fire at that one. To add a delay, you should run a function straight after getting the enemyCube and add a yield WaitForSeconds(1); at the top of that function. You will need that function to then pass the enemyCube var to the autogun so it knows what to fire at.

You can add a range as well using the distance between the 2 objects, and just add the if condition for the range

On autoguns script

 var dist=enemyCube.transform.position-transform.position;
 if(dist.magnitude<=range)Fire();
avatar image hathol · May 16, 2012 at 07:57 PM 2
Share

use

 if(dist.sqr$$anonymous$$agnitude<=range*range)Fire();

if you want to save performance, since the Sqrt() function that is used to calculate the exact magnitude is a lot more costly than a simple multiplication on the other side.

avatar image hijinxbassist · May 16, 2012 at 08:03 PM 0
Share

@hathol

Good to know, ive never used sqr$$anonymous$$ag b4...thanks for the explanation as well.

avatar image wknight92 · May 17, 2012 at 05:31 PM 0
Share

How would you go about storing the first cube in a variable?

avatar image wknight92 · May 17, 2012 at 06:25 PM 0
Share

hey hijinxbassist can you do a little explanation on your first code sample (if(enemyCube==null)enemyCube=collision.gameObject;)? Is this supposed to be attatched to the cube script? the autogun script? Any help understanding would be greatly appreciated. Thanks.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wknight92 · May 17, 2012 at 09:19 PM

Hey hijinx, that works great but for my purposes I was trying to do something a little different.

 function OnTriggerEnter (other : Collider){
     if (other.tag == "enemyCube"){
         yield Fire(other.gameObject);
 }
 }


 function Fire (cube : GameObject){
 yield WaitForSeconds (1.0);
 Destroy(cube);
 }

The pros of this : the cubes get destroyed in the order that they entered the 'range' of the collider(I want this to happen)

Cons: OnTriggerEnter is being called about every 10 milliseconds because of the speed of the Big Circle collider. So what ends up happening is the first cube is destroyed after the delay of 1 second and then all the other ones get destroyed almost instantly after that.

I want the collider to enter the cube network and destroy one cube per 1 second(for example). The order of cubes is important.

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

6 People are following this question.

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

Related Questions

OnTriggerEnter2D not working on second Trigger events 1 Answer

What books or other study material available for learning destruction physics of 2d terrains like Where's my water and sand balls ? 1 Answer

OnTriggerEnter 1 Answer

How can i detect hits from enemies inside of a player? 1 Answer

Method doesn't work when called in other 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