Random Trigger placement?

Im creating a little Horror Game. At one Point, there is a Trigger which plays a Sound.
But the Game will gets Boring if the Player know the Location of this Trigger. How can do,that the Trigger is every restart at another Point???

There is at least 2 ways you can place a trigger at random position:

1. Random.Range()

We can use this when we need the trigger to be in a specific room, but not a specific spot.

public class TriggerPlacement : MonoBehaviour {
   public Vector3 lowerlim, upperlim;

   void Start() {
      gameObject.transform.position = 
      new Vector3(
         Random.Range( lowerlim.x, upperlim.x ) + transform.position.x,
         Random.Range( lowerlim.y, upperlim.y ) + transform.position.y,
         Random.Range( lowerlim.z, upperlim.z ) + transform.position.z,
      );
   }
}

We can attached this to the trigger, it will then be placed in a random location limited by the lowerlim and upperlim.

Pros :

  • Easy to code

Cons :

  • Doesn’t take in account for rotation ( the area will always be a fixed rotation box )

  • Trigger may end up in inaccessible area

2. Transform Array

We can use this when we do not want the trigger’s position to be too random, or we need it to be at either one of few specific locations ( for example, it has to be located at one of the 3 doors )

public class TriggerPlacement : MonoBehaviour {
   public Transform[] location;
   public Collider triggerObject;

   void Start() {
      collider.gameObject.transform.position = 
      location[ Random.Range(0, location.Length) ];
   }
}

Pros :

  • More control on the in-game position of the trigger

Cons :

  • Harder to manage when you have a lots of possible location to place one trigger ( you may need to use custom Gizmos to help you manage these empty object in scene )