how to create triggers?

Hello,

How can I create a trigger, so if the player collide with the trigger a monster will spawn?

please advice.

Using the methods on Unity - Scripting API: Collider you can employ a wide variety of techniques.

thanks for the referance.

But let me ask my question in another way:

I’m developing a side scrolling game like Invaders. and I have 2 questions to asK:

1- how should I I trigger the enemies to be created from the right side on the screen in a waved ways? will triggers help here?

2- If the player did not kill the enemy ship, the enemy ship should get destoryed automatically after it become out of the screen? how to trigger this?

Thanks in advance.

  1. Example:
function OnTriggerEnter (other : Collider)
{
   Instantiate(...); // create an object where you want
}

2.1. You can add trigger behind you, which destroy not killed enemy.
2.2. Or you can add self destruction for object on creation:

function Awake()
{
   Destroy(gameObject,10); // enemy live 10 seconds, after that - he die
}

thank you for the quick reply.

As I understand that OnTriggerEnter is called when a collision happen between 2 objects.

now I thenking about a way to implement this in my senario, where maybe I need to create a hidden box or line with collider, and when this box collide my spaceship I will spawn the enemies.

Any other ideas that can help.

Thanks in advance.

As an alternative to using a Collider as a trigger, if you want something to happen in a simple case like when an object goes off the screen, you can just have the script for that object check for it in OnUpdate(). Especially since you’re doing a side-scroller, maybe you just need to check if the x position of the object is less then the left side of the screen, or something like that. Then you don’t need that collider for the trigger area and you don’t need a rigidbody for the object.

excellent Idea Can you please inform me how to get the position of the left or right side of the screen, as you know this game is a side scroller and the position of the screen keep changing?

I haven’t worked on any 2D games so I’m sure someone else can advise you better (and I’d guess the 2D Tutorial would have something, but I don’t remember). In any case I’m going to suggest you can do something with the ScreenToWorldPoint function, probably mentioned in a bunch of threads - here’s one: http://forum.unity3d.com/viewtopic.php?t=17009

Edit: On second thought, to test if something is on/off screen, I suppose Camera.WorldToScreenPoint is more convenient.

Actually, it’s a lot easier to have a trigger box attached to the player ship on the front and the back. When the front trigger hits an enemy, it tells them to wake up. When the back trigger hits an enemy, it tells them to destroy themselves.