Search Unity

how to create triggers?

Discussion in 'Scripting' started by bravery, Jul 14, 2009.

  1. bravery

    bravery

    Joined:
    Mar 26, 2009
    Posts:
    270
    Hello,

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

    please advice.
     
  2. phuzzy

    phuzzy

    Joined:
    Feb 12, 2009
    Posts:
    31
  3. bravery

    bravery

    Joined:
    Mar 26, 2009
    Posts:
    270
    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.
     
  4. jaxas

    jaxas

    Joined:
    Mar 22, 2009
    Posts:
    57
    1. Example:
    Code (csharp):
    1.  
    2. function OnTriggerEnter (other : Collider)
    3. {
    4.    Instantiate(...); // create an object where you want
    5. }
    6.  
    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:
    Code (csharp):
    1.  
    2. function Awake()
    3. {
    4.    Destroy(gameObject,10); // enemy live 10 seconds, after that - he die
    5. }
    6.  
     
  5. bravery

    bravery

    Joined:
    Mar 26, 2009
    Posts:
    270
    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.
     
  6. technicat

    technicat

    Joined:
    Nov 22, 2006
    Posts:
    1,277
    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.
     
  7. bravery

    bravery

    Joined:
    Mar 26, 2009
    Posts:
    270
    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?
     
  8. technicat

    technicat

    Joined:
    Nov 22, 2006
    Posts:
    1,277
    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.
     
  9. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    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.