x


When player enters triggerzone call function

Hi,

I seem to have a problem when entering a triggerzone. What i am trying to achieve is, whenever the player enters a certain zone (a well lit zone) What i tried to do is, put a sphere collider on the point light and attach this script:

var playerSanity : playerSanity;

function OnTriggerEnter(other : Collider) {

if(other.gameObject.CompareTag("Player"))
{
    Debug.Log("TEST");
    playerSanity.RegenerateFear();
}

Also use the collider as a trigger.

But this doesn't work. I also tried some tutorials that i could find on google. But they used the same method and didn't work either. Does anyone know a way how to achieve this?

Thanks,

Darryl

more ▼

asked Jul 25 '12 at 06:46 PM

darryldonohoe gravatar image

darryldonohoe
18 2 4 6

Is it possible that if i purchase Playmaker, that this would work? Cause i saw a tutorial which explains basically what i need. But if Playmaker uses the same method, there will probably be a high risk that it will not work. But if it uses another method, than it probably will. Because i still don't know what the problem is. I just cant figure it out.

Aug 06 '12 at 07:30 AM darryldonohoe

Try changing it to

Debug.Log(other.gameObject);

Debug.Log(gameObject);

you must have something else in the scene tagged as player, or something else with this script attached, or MAYBE your collider is scaled too big, try lowering the size of the collider component on the object

I don't know about Playmaker but this is a solvable issue, some detail is being overlooked is all

am I correct that the issue is still it activates on Start? this can ONLY mean the trigger is in contact with something tagged as Player, maybe the light, or one of its components

see what the above debug tells you

Aug 06 '12 at 07:42 AM Seth Bergman

I will take a look maybe you are right, that there are multiple objects tagged as player. Or something else. I'll also check the Debug.Log, what is it tells me. And once i have that checked. I'll post results here.

Thank you!

Aug 06 '12 at 09:05 AM darryldonohoe

no prob, hang in there!

by the way, something else I noticed since my prior advice..

setting the trigger to "isKinematic" is a BAD idea, leave isKinematic unchecked, and be sure to turn off gravity, otherwise I've found the trigger won't work unless it MOVES (collision matrix doesn't mention that part)

but only an issue if the player does NOT have a rigidbody

otherwise, I find the collision matrix to be a very useful guide

Aug 06 '12 at 09:21 AM Seth Bergman

Well i got it to work finally! I used the standard method. Same setup, different project. I think it had some problems with a script or an object in my old project. Cause right now it works like a charm. And i can finally wrap this feature up. Thanks for helping my out on this one Seth!

Aug 15 '12 at 09:47 PM darryldonohoe
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
    var playerSanity : playerSanity;  //type and identifier same? If the data type is 
                               //"playerSanity", the var name needs to be something different

    function OnTriggerEnter(other : Collider) {

   Debug.Log("entered trigger"); // see if the trigger was entered before checking tag

    if(other.CompareTag("Player")) //don't need "gameObject"
    {
        Debug.Log("TEST");
        playerSanity.RegenerateFear();
    }

}

not sure what the problem is though. See if adding a Debug.Log as above gets called, to rule out issues with the tag..

EDIT : sorry, forgot to mention before

You also need to have a rigidbody on one of the objects..

http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

the matrix at the bottom of this link will explain what types of objects can send trigger message

just add a rigidbody to the trigger object and set "isKinematic" true

EDIT: now I think DO NOT set isKinematic true, this may cause the trigger not to work (unless it moves) as described by my experience here

but that's only if the player does NOT have a rigidbody, I believe

more ▼

answered Jul 25 '12 at 07:06 PM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

I tried your script, but it didn't worked either. I can walk through the trigger collider, but the debug log before tag is checked isn't showing up. I thought something like this, was supposed to be simple.

Thanks anyway, i am going back to google. See if i can find any tutorials or reference scripts.

Jul 25 '12 at 08:59 PM darryldonohoe

I just tested the ones on the reference site, they didn't worked either. I also tried to use OnControllerColliderHit on the character controller. But the same problem. I tested the reference ones on the Player itself and on other game objects. It shows no debug log or anything.

Jul 25 '12 at 09:39 PM darryldonohoe

You also need to have a rigidbody on one of the objects..

http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

the matrix at the bottom of this link will explain what types of objects can send trigger message

just add a rigidbody to the trigger object and set "isKinematic" true

Jul 25 '12 at 09:43 PM Seth Bergman

The only one that is sort of working is the kinematic one. I tried this a few hours ago, but it gave me a message even though i was not in the trigger zone. It also didn't respond when i entered.

Jul 25 '12 at 09:54 PM darryldonohoe

sounds like the collider was just in the wrong position somehow. that's definitely the way to accomplish this

try again, and keep an eye on where the collider is, the only way you entered that function is by hitting a trigger

Jul 25 '12 at 10:11 PM Seth Bergman
(comments are locked)
10|3000 characters needed characters left

Make sure 'Is Trigger' is checked.

more ▼

answered Jul 26 '12 at 09:53 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

(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:

x336
x282
x101

asked: Jul 25 '12 at 06:46 PM

Seen: 633 times

Last Updated: Aug 15 '12 at 10:32 PM