x


OnTriggerEnter Question

Alright this should be a fairly simple question: I have a object so when (the problem) something enters, it runs a script using OnTiggerEnter.The problem with is the fact that it runs no matter what enters. So if a stray bullet goes into it, it runs. When a enemy wonders into, it runs. I know HOW to fix it, i just don't know the syntax for it, as i am fairly new to JavaScript and Unity. Well here is the script. Also, as a bonus point, if you know about arrays, could you help me make the script better? I want to make it so i don't have to write instantiate for every single thing.

    /*the variables, i would like for 
    spawnobject to be a array but i don't how to do them
    */
    var spawnobject : Transform;
    // player1 i would set as the player
    var player1 : Transform;

    //the function for it
    function OnTriggerEnter () {
    /* i want it so that if it was player1 
   that entered it, that it will run the 'if' i dont 
   know how to make it say that it was player 1
   */
    if (player1) {
    Instantiate(spawnobject, GameObject.Find("BallSpawn").transform.position, Quaternion.identity);

    Instantiate(spawnobject, GameObject.Find("BallSpawn1").transform.position, Quaternion.identity);

    Instantiate(spawnobject, GameObject.Find("BallSpawn2").transform.position, Quaternion.identity);

    Destroy(this,0.0);
    } 
    }
more ▼

asked Feb 15 '10 at 02:36 AM

xToxicInferno gravatar image

xToxicInferno
485 24 28 41

I kicked out two of your tags as they were unrelated to the actual problem. Please correct the typo in your question title so that other people can find your question more easily. Thanks!

Feb 15 '10 at 02:55 AM Sebas
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

straight from the documentation:

// Destroy everything that enters the trigger
function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject);
}

As you can see there, the OnTriggerEnter function accepts a collider as parameter. Basically, it tells you exactly which object entered the trigger. You can check "other" and see whether it is the player or a bullet or whatever which entered your trigger. Possibly check for the tag of your entering collider and only trigger something when the tag of your entering collider is e.g. "player"

again from the documentation:

gameObject.tag = "Player";

Combine the two script examples and you should be all set

That should result in a statement like such in your OnTriggerEnter function:

if (other.gameObject.tag == "Player") {
  //do stuff here
}
more ▼

answered Feb 15 '10 at 02:43 AM

Sebas gravatar image

Sebas
4.1k 12 18 45

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1015
x71

asked: Feb 15 '10 at 02:36 AM

Seen: 10529 times

Last Updated: Feb 15 '10 at 05:02 AM