0
\$\begingroup\$

I am completely new to Unity 3D, just started to follow some tutorials y'day. I am trying to build a 2D car racing game as a part of my learning (time beating, no opponents). I have a racing track as texture (repeating). My problem is knowing when the car is outside/inside the track (I wouldn't prefer to limit it to the track boundaries).

What I have tried after searching around : I read somewhere about Mesh Colliders so:

  • I added a mesh collider to the track (with 'Convex' and 'Is Trigger' checked , to be honest I don't know what Convex is, but I couldn't check 'Is Trigger' w/out checking 'Convex').

  • Added OnTriggerExit and OnTriggerEnter methods to the Track script (the script that scrolls the track) hoping the two would be called each timer the car goes out of the track or gets back in.

Apparently the two methods aren't called at all, Would someone please help. Thanks.

\$\endgroup\$
  • \$\begingroup\$ i would have a trigger placed outside the track, rather than being the track. sometimes unity runs into issues if you start the character model in the collider it wants to detect. So a simple test would be to put the car outside the track and have him come onto the track. \$\endgroup\$ – Ryan white Jan 17 '17 at 14:56
  • \$\begingroup\$ For future reference, a "convex" collider uses the convex hull of the mesh for collisions. That means any crevices or concavities get flattened out, so the mesh has only flat regions and outward bulges, never inward, and has no holes or hollows — just one big blob. If I had a collider shaped like a hand, using its convex hull would turn it into a paddle. It's more efficient for collisions with complicated meshes with unimportant detail, but it's not what you want for representing all the bends and valleys in a race track. \$\endgroup\$ – DMGregory Sep 19 '17 at 11:44
2
\$\begingroup\$

I can think of 3 solutions of the top of my head i'll list them by simplicity (in my opinion):

  1. You can check what texture is currently under the car, and test it against textures that are considered "track" textures, if not then its out otherwise its in.
  2. You can use Triggers along the track and check if the car is in at least 1 trigger, being in one or more triggers means its inside the track, otherwise its outside of it, for this you will have to get the triggers info from a script bound to each of the empty objects with triggers attached to a final bigger script (one way for example).
  3. Writing a code that can run through the terrain data, and check everywhere that is track (probably using textures again), pre process that data and just check the car position relative to your pre processed data.
\$\endgroup\$
0
\$\begingroup\$

Maybe it's a typo, watch out for Capital letters. Did you assign any name to the Trigger collided with?

If you just write OnTriggerEnter(){...} Or OnTriggerExit(){...} it won't work. You need a name for the Collider that triggered it. so for example OnTriggerEnter(Collider2D Player){...} Or OnTriggerExit(Collider2D Player){...}.

If you did that, it should at least be called.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.