Road problem. Detect collision without effectively colliding with a rigidBody
I will explain my problem, I have a road, and I have a player, on the player is attached a rigidbody which is not kinematic. It's not kinematic because around the road they are walls and I don't want to pass throught them. But when I leave the road with my player I want Unity to show a message like : ( " Hey you leaved the road!"). That was for the context. My player movement is given by the speed of my mouse like this : rigidBody.velocity = mouseSpeed. And I do this with the objective that if my character meet a wall it will be stopped, but the problem is when I try to drive on the road my character is blocked on the side, exactly like if my road was a wall...
So the solutions that I imagined was to ignore the collisions between the player collider and the road collider (with the matrix collision), but if I do this the message is never send (but I can drive on the road)... Another solution was to use the method Physics.IgnoreCollision, if I do this I can drive on the road but the message is also never send...
Is someone know how detect collision without effectively colliding with a rigidBody?
Answer by eses · Aug 21, 2018 at 02:56 PM
Hi @unity_Yix4cg1p_JpgwQ
Your question is bit hard to understand.
You talk about player not being able to pass through some colliders, but then you say he should be able to pass through them, so that you can detect off the road event.
...I'll assume, you meant that you want to detect when your player is outside the track area, when he drives through these colliders.
You'll need to just use Triggers instead of Collisions. Set your road boundaries to triggers, when your player goes past this no-go line, these colliders will fire their OnTriggerEnter... then do whatever you need to do.
See documentation:
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
I already tried to use OnTriggerEnter/Exit, but I can't use it because I want to have an access to the normal of the walls that I hit outside the road.
$$anonymous$$y problem is : there is a collider on the road and I want to pass throught it but I want to be adviced when my player is on the road or not. I don't know if my question is clear, or if I can do this with Unity.
Thank you for your answer.
PS: I am French, this is why my english is not perfect, sorry...