x


boolean flicker

I'm using a raycast detection to activate a boolean function in another script. This boolean then turns on or off a OnGUI screen. The problem that I'm having is when the state is true, the OnGUI flashes on/off like it's switching between true/false rapidly.

Is there a way that I can only call the function when the collision state changes, not on every frame?

From the collider script:

function Update()
{   
    var hit : RaycastHit;
    // check if we're colliding
    if(Physics.Raycast(transform.position, transform.forward, hit, speakarea))
    {
        // with a dog
        if(hit.collider.gameObject.tag == "dog")
        {
        otherScript = NPCObject;
        otherScript.ShowIntro();
        }
    }
    else{
        otherScript = NPCObject;
        otherScript.HideIntro();
        }
}

From the OnGUI script:

function ShowIntro()
{
showIntro = true;
}

function HideIntro()
{
showIntro = false;
}

function OnGUI()
{
    if (showIntro) {
            GUI.Box (Rect (Screen.width * .2 ,Screen.height * .2,240,360), "");}

}

more ▼

asked Jul 08 '10 at 02:03 PM

TinyUtopia gravatar image

TinyUtopia
270 22 26 38

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

2 answers: sort voted first

You can try using OnTriggerStay() instead of ray casting to check if you're within range of the dog and attach a sphere collder to it.

If you only want the GUI to appear when you're facing the dog and you try a combination of rays and triggers so that if you are a) facing the dag and b) within the sphere collider then showIntro is true.

If you leave the trigger showIntro = false so that once the GUI is on it'll stay on until you walk away even if you turn the player around.

e.g.

function OnTriggerExit (other : Collider) {
   if(other.gameObject.CompareTag("dog")){
      HideIntro();
   }
}
more ▼

answered Jul 08 '10 at 02:21 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

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

Solved this by doing like spinaljack said and added another boolean.

function OnTriggerStay () { 
    insideTrigger = true;
}

function OnTriggerExit (other : Collider) {
  if(other.gameObject.CompareTag("dog")){
    insideTrigger = false;
    NPCObject.HideIntro();
    }
}
more ▼

answered Jul 08 '10 at 03:31 PM

TinyUtopia gravatar image

TinyUtopia
270 22 26 38

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

x386
x242
x240
x46

asked: Jul 08 '10 at 02:03 PM

Seen: 1016 times

Last Updated: Jul 08 '10 at 02:03 PM