Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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:
    59
    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. Deleted User

    Deleted User

    Guest

    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. Deleted User

    Deleted User

    Guest

    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,571
    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.