x


Varaible not decementing OnTriggerExit

Hi,

I added a rigid body to my player that I can control and move around the arena. I have the following C# Script which detects if my player has 'stayed' or 'exited' the Plane game object:

public float x = 0;

//...

void OnTriggerStay(Collider other)
{
   if ( x <= 50 ) x++;
}

void OnTriggerExit(Collider other) 
{
  // does happen when below is commented but not vise versa
  Debug.Log("outside of Plane");  

  //if ( x < 50 && > 0 ) x--;
}

Just wondering why.

What I don't want to do is this:

//...
bool isHere = false; 
void UpDate()
{
 if (isHere == false) x--;
}
void OnTriggerStay(Collider other)
{
   isHere = true;
}
void OnTriggerExit(Collider other) 
{
   Debug.Log("leaving plane..");
   isHere = false;
}

Which works, but can this be done only in the subrountine OnTriggerExit? I am all ears.

more ▼

asked May 22 '10 at 04:08 AM

lampshade gravatar image

lampshade
391 84 92 109

If you don't want the Update, you could always just start a coroutine with OnTriggerEnter and stop the coroutine with OnTriggerExit.

May 22 '10 at 05:17 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

It seems like you think "OnTriggerExit" is called repeatedly if the object is not in the trigger. That is not the case, as it is only called once on the same frame the object leaves the trigger.

Therefore you will have to use a boolean to control the logic flow similar to what you have in the second set of code. Also if this is for points or something similar you should use something like Time.time or Time.deltaTime to do this over time, otherwise it will happen every frame causing the incrementing/decrementing to be frame dependent rather than time dependent, and very fast in most cases(shooting to 50 or zero in less than a second).

more ▼

answered May 22 '10 at 04:38 AM

dhendrix gravatar image

dhendrix
2.2k 25 34 59

Awesome post thanks so much =)

May 22 '10 at 04:59 AM lampshade
(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:

x45

asked: May 22 '10 at 04:08 AM

Seen: 865 times

Last Updated: May 22 '10 at 04:28 AM