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 TheFrozenFires · Sep 02, 2013 at 02:23 PM · collisiontriggershooter

trigger collision with three triggers help

Hello

I am making a top scrolling shooter following this tutorial http://walkerboystudio.com/html/unity_course_lab_2.html

 function OnTriggerEnter (other : Collider)
 {    
     if (other.gameObject.tag == "Bullet")
     {
         sceneManager.transform.GetComponent("script scene manager").AddScore(); //Adds a point for the astroid being hit
         if (explosion) //Check for loaded explosion
         {
             Instantiate (explosion, transform.position, transform.rotation);//Create Explosion
             audio.clip = playerDamage;
             audio.Play();
         }
         ResetPosition(); //Resets the Astroid position
     }
 }

I have finished following the videos and am now doing the on your own section however I am running into a few problems. When I created the extra socket to fire another bullet, this instantiates a second bullet. However when two bullets collide with the asteroid at the same time it called the OnTriggerEnter function twice which will add two points, when only one should be awarded. Is there any way I can make it so that if two bullets collide at the same time the OnTriggerEnter is only called once, or if there is some alternate solution I haven't thought of.

Any help would be greatly appreciated :)

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 ShadoX · Sep 02, 2013 at 03:18 PM 0
Share

I don't think that there is really something like "at the same time". Even if it looks like that to you, chances are that there is a split second difference so you could try using a timer to check if the the OnTriggerEvent has been already called or perhaps a simple boolean variable would be enough?

 Call onTriggerEvent > boolean = true
 if(boolean==true){
 //destroy bullet?
 }

something like that might work

[edit] If there really is a chance of the bullets hitting the object at the exact same time you could try checking which object they interacted with and then add the score. In this case you could probably use a different (global?) script that (for example) contains a array with only unique entries. When you git a Astroid, you could add that to the list and do the rest as is.. and then go over that list and just add points for each astroid in the list as long as the list only contains unique entries.

avatar image TheFrozenFires · Sep 02, 2013 at 05:15 PM 0
Share

I tried placing a print(Time.time); in to test, and it only prints once, so OnTriggerEnter is only being called once, but two points are still added

avatar image DESTRUKTORR · Sep 02, 2013 at 05:18 PM 0
Share

Are you certain that it's only calling it once? You might have "collapse" marked in your console. Check to see if there's a little number on the right end of the printout. If it's a 2, it's being called twice.

$$anonymous$$eep in $$anonymous$$d that Time.time doesn't change until the next frame, so no matter how long it takes for any one frame to completely process, it will not change the time until the next frame is loaded.

Also, it's a little off topic, but might I suggest not using "GetComponent" for a component that is constant? You should have an instance variable for that, and just have it saved before or just after the scene loads, so that it doesn't have to go searching for it every time (it's notably costly, computationally).

avatar image TheFrozenFires · Sep 02, 2013 at 05:55 PM 0
Share

Your right collapse was checked, it is called twice

So I should just make the variable scene manager do get component when I declare it at the start of the script? This will avoid it calling it any time a collision happens?

avatar image DESTRUKTORR · Sep 02, 2013 at 07:01 PM 0
Share

You don't necessarily need to do GetComponent at all. If you have an instance variable in your script that is of the same type as the component you want to continuously reference, you can drag and drop it into that slot, in the editor, and it will remember to use that object in the reference, in play-mode.

For example:

  var scene$$anonymous$$anager : ScriptScene$$anonymous$$anager;// Assu$$anonymous$$g that was the type name for that class

If you look at the component in the inspector, now, there should be a field labeled "Scene $$anonymous$$anager" that can hold a ScriptScene$$anonymous$$anager object. Simply drag and drop a GameObject with the ScriptScene$$anonymous$$anager on it into that slot (this even works if it is the same GameObject that this script is attached to).

0 Replies

· Add your reply
  • Sort: 

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

18 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

Related Questions

How to add to a 3D object's length through code? 1 Answer

Overlap point of 2 kinematic colliders 1 Answer

How to add the right amount of points when my player destroys a gameobject? 0 Answers

Receiving information about getting hit by a Raycast 2 Answers

How do you execute Trigger-collider collision only in one gameobject? 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