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 cassius · Aug 05, 2012 at 10:27 PM · colliderontriggerenter

OnTriggerEnter - Hit Only One Collider

I have an enemy that has multiple child objects, each with their own colliders. I am using their OnTriggerEnter events to update the player's stats. So basically, if the player's weapon impacts any of the enemy's colliders, they'll signal the stats script that the last weapon fire was a successful enemy hit. A shooting percentage.

My problem is that because the player's weapon fire goes so fast, it will sometimes collide with 2 or more enemy child object colliders within the same frame/Update. As a result, each of the impacted colliders sends the signal that a successful hit occured. That's problematic if, say, the player fired just 1 shot and technically impacted 2 or 3 colliders, giving them a 200% or 300% shooting average.

I have the player's weapons set to disable immediately on a collision, so this is why I figure it's multiple hits within a single Update/frame. All relevant colliders are set to "Is Trigger".

Does anyone know of any ways I may be able to work around this?

Comment
Add comment · Show 6
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 TurboHermit · Aug 05, 2012 at 10:37 PM 0
Share

High speed physics have always been problematic in unity in my opinion. I suggest ins$$anonymous$$d of actually shooting projectiles, you use raycast.hit to indicate what collider you're hitting.

But don't let me hold you back, I have an 80% chance of not knowing what I'm talking about.

avatar image reptilebeats · Aug 05, 2012 at 10:49 PM 0
Share

test for all the hits and only use the first one, so you could have a varible which is empty and if varible is empty then it equals the first hit, then after a second remove the varible again. you also need to destry the bullet or collider on impact, or stop checking for a hit with that object.

this is just of the top of my head and is all theory but it might give you a hand with what you are doing.

avatar image Palimon reptilebeats · Oct 10, 2015 at 01:29 AM 0
Share

Hey, fyi, for anyone else who comes by, I had trouble with missiles hitting multiple colliders in a single frame (hitting shields and the subsystems below the shields) and this solution worked perfectly for me. I just put a bool on the script for whether it's hit a trigger or not, and only do my missile-hit code on the first trigger.

avatar image cassius · Aug 06, 2012 at 01:35 AM 0
Share

Danzou,

Do you mean a combination of raycast and my physical projectile? I've never dealt with raycast yet. Are they visible to the player? The reason I'm using a projectile is because the player sees the weapon they're shooting.

In my game the player is constantly moving forward at a decent pace. When they fire, the weapon of course goes faster forward than the player's pace. However in relation to worldspace, the projectile is moving quite fast.

Do you still think this is something raycast may help with somehow?

avatar image cassius · Aug 09, 2012 at 12:06 AM 0
Share

Just following-up on my OP to say that I still haven't found a solution for this. So if anyone has any ideas, I'd be happy to know :)

avatar image cassius · Aug 09, 2012 at 07:33 PM 0
Share

Upon further thought, I'm wondering if the OnTriggerEnter() function will trigger more than once in a single frame (Update) if there are multiple colliders just entering it. Or will it just trigger once per Update() no matter what and keep reference to each collider, that has just entered, in a single OnTriggerEnter pass. If it's the latter then perhaps there's a way to know how many objects just entered the collider this current Update()?

That was long-winded. Hope it made sense.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by thedodgeruk · Aug 06, 2012 at 01:20 AM

you can just delete the object if it hits a collider. this is what i use with projectile weapons

 void OnCollisionEnter(Collision collision)
 {


     if (collision.gameObject.name == gameObject.name) //cant collide with its own kind
         return;


     collision.gameObject.SendMessageUpwards("ApplyDirection",   transform,  SendMessageOptions.DontRequireReceiver);
     collision.gameObject.SendMessageUpwards("ApplyDamage",      m_damage,   SendMessageOptions.DontRequireReceiver);

     if (m_impactObject)
         Instantiate(m_impactObject, transform.position, transform.rotation);


     if (m_impactAudioClip)
         AudioSource.PlayClipAtPoint(m_impactAudioClip, transform.position);
     if (rigidbody)
         Destroy(rigidbody);

   

     GameObject.Destroy(this.gameObject);


 }
Comment
Add comment · Show 4 · 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 cassius · Aug 06, 2012 at 01:32 AM 0
Share

Thanks for the reply, but as mentioned I'm already disabling the object on the collision. The issue is that, because of the speed of the projectile, it can collide with multiple objects within a single Update.

So for example, if my projectile is moving at a distance of 2 units every frame (60 units/second at 30FPS) I may have 2 enemy child objects within a distance of 0.5 units of each other. The problem is that at say, frame 0, no enemy child objects are impacted, whereas by frame 1 the projectile is physically located within the space of 2 or 3 enemy child colliders. So even when I disable on the collision it has already impacted multiple colliders, and therefore each of those colliders will then signal to the controller script that a successful weapons impact has occurred.

Hope that makes sense.

avatar image Ingen · Aug 09, 2012 at 12:14 AM 0
Share

surry how much is big the projectile, maybe use a rayCast ins$$anonymous$$d the bullet can solve,

avatar image cassius · Aug 09, 2012 at 12:18 AM 0
Share

But are rayCasts a visible object like a mesh is?

$$anonymous$$y player's weapon (laser - think Star Wars blaster style) has to be clearly visible by the player.

To see what I mean about the weapons (not the problem itself), there's a video I posted a few days ago at http://www.superspacetrooper.com/2012/08/sst-desertia-level-video-released-work-in-progress/

avatar image Ingen · Aug 09, 2012 at 01:04 AM 0
Share

for what I know Raycast is never visible, and line draw, draw a line along all ray, and you don't want this

avatar image
0

Answer by IshikiGames · Oct 10, 2015 at 10:15 AM

Instead of resolving destruction on projectile collision, have something track the position of the projectile each frame. Compute based on the velocity of your projectile if a collision will occur next frame using a raycast, if a collision will occur, destroy the projectile and the raycast result.

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

13 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

Related Questions

How to make gameObject make only one collision 1 Answer

Character controller collider? 0 Answers

Check trigger/collider once? 1 Answer

OnTriggerEnter2d() Not Triggering While Animating. 1 Answer

Layers, parenting and collisions. 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