- Home /
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), "");}
}
Answer by spinaljack · Jul 08, 2010 at 02:21 PM
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();
}
}
Answer by TinyUtopia · Jul 08, 2010 at 03:31 PM
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(); } }
Answer by DFLY · Mar 15, 2014 at 05:55 AM
Hi TinyUtopia
I was searching for the same answer and came across your question. I have just solved it by using an intermediate boolean like this:
private var touched: boolean;
private var touching: boolean;
function Update(){
var ray = cam.ScreenPointToRay (Vector2 (Screen.width*0.5, Screen.height *0.5));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 20) && hit.collider == yourTrigger ) {
rayTouching = true;
}
else {
rayTouching = false;
}
if (rayTouching && !rayTouched){
DoSomething();
rayTouched = true;
}
if (!rayTouching && rayTouched){
DoSomethingElse();
rayTouched = false;
}
}
I know it's way later than you question. But other people with this same problem will find this useful.