0
\$\begingroup\$

I'm developing a simple racing game, using Unity. I've created a road simply applying a texture (asphalt) to a terrain . Is it possible (and how to do) to mark that roard with an invisible marker to identifiy that as a road (so i can make appears bonus, or others similar things) ? Which is the approach used in a racing game to accomplish that task ? Please, don't tell me "buy a Roads assets" because i want to understand the basics, the main concept.

Thanks

\$\endgroup\$
  • \$\begingroup\$ you can add a Tag with the name Road to the terrain object and use that to identify it. I'm not sure if this is what you want. \$\endgroup\$ – Uri Popov May 3 '16 at 10:21
  • \$\begingroup\$ mmmm.. no, because : 1) i'm using a texture to 'draw' the road (is it the best way?) 2) i need to mark the terrain to make appears bonus random on roads \$\endgroup\$ – stighy May 3 '16 at 10:36
0
\$\begingroup\$

Usually when you want to have "Zones" in unity, you use game objects with Colliders with isTrigger activated.

There are two strategies you can use here. Either you surround the track with colliders to detect when the vehicle leaves the road (OnTriggerEnter) or you place colliders on the road and detect if the vehicle is still on the road (OnTriggerStay).

When you use the latter method, keep in mind that when you create your road from multiple colliders then it is possible that a vehicle triggers multiple OnTriggerStay events in the same frame. To handle this problem I would recommend you to have the OnTriggerStay method of the road colliders set a flag on the vehicle which is then retrieved in the update-method of the car.

public class RoadMarker : MonoBehavior
{
      void OnTriggerStay(Collider other) {
          Vehicle vehicle = other.GetComponent<Vehicle>();
          if (vehicle != null) {
               vehicle.isOnRoad = true;
          }
      }
}

public class Vehicle : MonoBehavior
{
      public bool isOnRoad;

      void Update() {
          if (isOnRoad) {
              // do what you want to do when vehicle is on the road
          } else {
              // do what you want to do when vehicle is not on the road
          }              
          isOnRoad = false; // to be changed by RoadMarker... or not
      }
}

Regarding placing object on the road: You could make each RoadMarker responsible for handling object spawns within its collider.

\$\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.