- Home /
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?
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.
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.
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.
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?
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 :)
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.
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);
}
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.
surry how much is big the projectile, maybe use a rayCast ins$$anonymous$$d the bullet can solve,
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/
for what I know Raycast is never visible, and line draw, draw a line along all ray, and you don't want this
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.
Your answer
Follow this Question
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