- Home /
is it possible to not get triggered by your own BoxCollider ?
I have a problem, i have 2 car (the red one is my player) they both have a boxCollider attache to their gameObject and i want to stop the car who enter the boxCollider of the other one (because the red is faster than the grey). Here is an exemple schema:
I want my red car to stop when it enters the Collider box of the grey car but not my grey car to stop. I would also like the grey car to stop when it enters the Collider box of my red car ( player).
The problem is when the red car enters the boxcollider of the grey car both stops because the boxCollider also triggers the grey car and not only the red one. I would therefore like the trigger of the boxCollider not to trigger the GameObject with which it is associated. Is that possible? Or is there an oder solution
Here is the code of the red car(player):
void OnTriggerExit(Collider other)
{
if (other.tag == "TrafficCar")
{
targetSpeed = 14f;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "TrafficCar")
{
targetSpeed = 0f;
}
}
the grey car have the same code, only the tag change. Any help would be aprecieted !
Answer by GanemVisk · Dec 13, 2019 at 02:41 PM
Have a reference to your own collider and check if it's it.
public Collider ownCollider;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "TrafficCar" && other != ownCollider)
{
targetSpeed = 0f;
}
}
Edit: or if the collider is in the same game object as the script you can check that (which was the question): other.gameObject != gameObject
it could have been a good idea but unfortunately it doesn't work...
I tested a simplified version and it worked. Did you put the Collider in the ownCollider box in the editor?
Ofc, but I have an old version of unity (5.6.2) due to old asset and I can't upgrade. But I will dig a bit more on this one.
The problem with this solution, is it assumes the car may be colliding with itself, and this will solve that problem, if that is the case, however, I don't think this is the problem that is going on. When an object moves into a trigger collider, BOTH objects will get the OnTriggerEnter, so the grey car will see the red car colliding, and the red car will see the grey car colliding. The problem to solve here is how to deter$$anonymous$$e which car needs to slow down, when both cars are receiving the trigger.
Answer by Larry-Dietz · Dec 13, 2019 at 03:47 PM
Easiest way I can think of to handle this, without a ton of velocity checks, etc... would be to modify your OnTriggerEnter function to see if the colliding object is in front of or behind your transform position, and only run your slow down code is the colliding object is in front of your position.
This way, using your image as an example, Grey car detects trigger, sees collision is occurring behind it, discards collision(does nothing) Red car detects trigger, sees collision is occurring in front of it, slows down.
Hope this helps, -Larry
Great idea but i am a newbi in unity... Do you have and example code of this case ? This could be very helpful !
Is this an endless runner type game? Are the cars always moving in the direction of your image?
If so, assu$$anonymous$$g this is a 2D game, like image, then you can just compare x positions. Something like this...
private void OnTriggerEnter(Collider other)
{
if (other.transform.position.x < transform.position.x)
{
targetSpeed = 0f;
}
}
Tank you ! No, it's a 3d simulation and the grey car is the traffic (~1000 other cars like this in the simulator )
Answer by Philosophbot · Dec 13, 2019 at 03:27 PM
In the if statement you can check to see if the velocity of the car is greater than the one that you are colliding with. private void OnTriggerEnter(Collider other){ if (other.tag == "TrafficCar" && this.getComponent().Velocity > other.gameobject.getComponent().Velocity){ targetSpeed = 0f; } }
or if you aren't applying forces you can just check to see if the targetSpeed
value is greater than the other cars targetSpeed
value.
Your answer
