x


Why is this code giving me an error... lots of them

i dont get why this script is giving me a lot of errors its like right out of the reference material. i am thinking it has to do with the if and the on trigger, but i could be completely wrong any help?

   function Update () {

    if(gameObject.tag == "enemy")
       {
       function OnTriggerEnter(other : Collider)
       {
       transform.LookAt(gameObject); 
       }
       }

}

after i did the above code i looked around and found my errors..... i think... but i am still having trouble any help?

function Update () {

var enemy = GameObject.FindWithTag("enemy");
var trigger = false;

OnTriggerEnter(enemy);

OnTriggerExit(enemy);

    if(trigger == true){

       transform.LookAt(enemy);
    }
}

function OnTriggerEnter(enemy : Collider){

    return trigger = true;
}

function OnTriggerExit(enemy : Collider){

    return trigger = false;
}
more ▼

asked Jul 29 '11 at 06:47 AM

IMTRIGGERHAPPY9 gravatar image

IMTRIGGERHAPPY9
132 29 43 44

"its like right out of the reference material", could you please provide a link to the reference material because that looks wrong and should be corrected.

Jul 29 '11 at 11:24 AM Statement ♦♦
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Your code seems to do nothing since you're trying to have the object to look at itself.

This is my best guess for what you want to do:

function OnTriggerEnter(other : Collider)
{
    if (other.tag == "enemy") // If I entered trigger of an enemy
    {
        transform.LookAt(other.transform); // Then I will look at that trigger
    }
}

In this solution there is no Update function at all.

more ▼

answered Jul 29 '11 at 11:23 AM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

state is still alive :O

Jul 29 '11 at 11:25 AM biohazard

i guess i am looking at the wrong material for what i want to do, but these are great examples thanks for the help but they didn't solve my problem.

Jul 29 '11 at 06:42 PM IMTRIGGERHAPPY9
(comments are locked)
10|3000 characters needed characters left

Something like the following (which hasn't been compiled so expect a few typos):

static var doit = false;

function Update () {

    if(gameObject.tag == "enemy") {

        doit = true;
    }
    else  {
        doit = false;
    }
}

function OnTriggerEnter(other : Collider)
{
    if (doit) {
        transform.LookAt(gameObject);
    }
}
more ▼

answered Jul 29 '11 at 09:33 AM

Graham Dunnett gravatar image

Graham Dunnett ♦♦
11.9k 11 20 64

(comments are locked)
10|3000 characters needed characters left

Because the function OnTriggerEnter is an own function and must be outside of the Update function where you put it in by error

more ▼

answered Jul 29 '11 at 06:48 AM

Dreamora gravatar image

Dreamora
3.2k 1 4 25

so the OnTriggerEnter is like the update where it looks for it every frame right? well then how would i go about doing this because i want my if to control that function, can you even have if's control functions?

Jul 29 '11 at 06:50 AM IMTRIGGERHAPPY9

Yes. it will keep checking if the trigger was hit UNTIL it was hit, i think you might have to reset it then but i don't really know.

Alternatively you could try working with Raycasts and collider

Jul 29 '11 at 07:14 AM biohazard
(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:

x329
x38

asked: Jul 29 '11 at 06:47 AM

Seen: 415 times

Last Updated: Jul 31 '11 at 12:34 AM