Hi all,
I’m a newbie with unity.
I’m working on a car AI.
I want the cars to turn to another direction when they enter a specific area.
As I’ve finished Lerpz 3d tutorial, what I can think of is to use a box collider object in that area. I thought I could trigger this if this box is on the same plane as track. But it didn’t work until I raised it a little to get the car collide with it. So, the track is not looking good since it has a box across it.
How can I fix this?
Any other ways to get it working?
My main objective is to make the cars automatically driving in the track with various speeds.
Greatly appreciate for any ideas.
Thanks,
Sithu
1 Like
You could use that box collider and check the “is Trigger” property. That way when your cars drive through it, it will register as OnTriggerEnter, Stay and Exit events. If you can see the box just simply uncheck the “Mesh Renderer” component or delete the “Mesh Renderer” component from it.
To detect a trigger, use the OnTriggerEnter, OnTriggerExit, and OnTriggerStay functions (whichever are appropriate for your particular case) in a script attached to your vehicles.
1 Like
Hi
Many thanks for your reply.
I tried exactly as you’ve said.
My problem is when I use box collider, I need to raise it a little above the track. If it’s on the same plane as the track, my OnTriggerEnter is not fired.
Any ideas?
Thanks,
Sithu
I said that wrong… the script needs to be attached to the trigger object.
I usually just put a print command inside to make sure it’s working.
Make sure the trigger/box collider is big enough for you cars to go through it.
function OnTriggerEnter(theCollider : Collider) {
print(theCollider.gameObject.name);
}
Also, make sure that you’re controlling your vehicles with physics functions like AddForce() or AddRelativeForce(), etc… Make sure you also have a rigidBody component on your vehicle. If you don’t do these two things, then the trigger won’t register a trigger event. If you use code like the following the trigger won’t work:
transform.position.z += forwardAmt * Time.deltaTime;
Instead use:
rigidbody.AddForce (Vector3.forward * forwardAmt * Time.deltaTime);
In the attached image I’ve shown how you may want the trigger to be placed on your track. Just make sure it’s big enough that vehicles can’t miss it.
Let me know if this helps,
Nathan

2 Likes
Got it.
Thank you so much.
Warm regards,
Sithu
Your welcome Sithu,
Just holler if you have any more questions. There are a ton of people who are about 100x smarter than me on here, but, I like helping whenever I can. 
Nathan