- Home /
How to detect if a raycast ray stop hitting an object
Hi, I need a way to understand in c# when a ray stop hitting an object.
 RaycastHit boostHit;
         Vector3 bottom = transform.TransformDirection (Vector3.down);
         
         bool prevFinishLane = finishLane;
         
         if (Physics.Raycast (transform.position, bottom, out boostHit)) {
             
             if (boostHit.collider.tag == "Start") {
                 finishLane = true;
             } else {
                 finishLane = false;
             }
             
             if (boostHit.collider.tag == "Boost") {
 
                 GetComponent<AudioSource>().PlayOneShot (boostSound);
                 GameManager.Instance.SetCheckedBoost (GameManager.Instance.GetCheckedBoost () + 1);
                 Debug.Log ("Boosts: " + GameManager.Instance.GetCheckedBoost ());
 
                 if (boostTimer == 0.0F) {
                     boost = true;
                     maxSpeed = GameManager.Instance.GetBoost ();
                     accel = 20.0F;
                 }
             }
 
 //            if (boostHit.collider.tag == "Brackes") {
 //
 //                prevSpeed = speed;
 //
 //                GetComponent<AudioSource>().PlayOneShot (brackesSound);
 //                if (decelTimer == 0.0F) {
 //                    brakeDecel = true;
 //                    decel = 40.0F;
 //                }
 //            }
         }
         else {
             finishLane = false;
         }
That's why in this way the boost collider returns a lot of fires. I need that the event returns only one impulse per passage. (Boost). So I thought to detect when tha ray leaves the object instead of when the ray hits the object... But I can't code that...
I assume you want to execute boost code block once?
If yes just use a bool as a trigger. Example : Assign a bool gotBoost = false at the start of script.
 if (boostHit.collider.tag == "Boost" && !gotBoost) {
 gotBoost = true;
  
                  GetComponent<AudioSource>().PlayOneShot (boostSound);
                  Game$$anonymous$$anager.Instance.SetCheckedBoost (Game$$anonymous$$anager.Instance.GetCheckedBoost () + 1);
                  Debug.Log ("Boosts: " + Game$$anonymous$$anager.Instance.GetCheckedBoost ());
  
                  if (boostTimer == 0.0F) {
                      boost = true;
                      maxSpeed = Game$$anonymous$$anager.Instance.GetBoost ();
                      accel = 20.0F;
                  }
              }
Later you will want to reset gotBoost back to false if want to execute Boost block again
It desn't seems to work...Whre I have to reset the gotBoost bool? Also I need to fire the audio event and the SetCheckBoost function only once...in this way it doesn't work.
I have to ask why you don't simply use OnTriggerEnter/OnTriggerExit?
To complete Priyanshu's idea:
  if (boostHit.collider.tag == "Boost") 
  {
      if(!gotBoost)
      {
          gotBoost = true;     
          GetComponent<AudioSource>().PlayOneShot (boostSound);
          Game$$anonymous$$anager.Instance.SetCheckedBoost (Game$$anonymous$$anager.Instance.GetCheckedBoost () + 1);
          Debug.Log ("Boosts: " + Game$$anonymous$$anager.Instance.GetCheckedBoost ());
   
           if (boostTimer == 0.0F) 
           {
               boost = true;
               maxSpeed = Game$$anonymous$$anager.Instance.GetBoost ();
               accel = 20.0F;
            }
        }
    }
 else
     gotBoost = false;
Now I''l try... I can't use OnTrigger events bacause i need the surface free from colliders to add the enemies AI...(I'ts a race game where ships navigate in a pipeline)
But you already have the colliders! Never$$anonymous$$d, glad we can help!
Answer by areFranz · Jul 19, 2015 at 07:05 PM
Thanks Guys!! It works great!
Here it is the complete solution:
 public bool boost;
 public boot gotBoost;
 public float boostTimer;
 
 // Other Speed, Audio and Accel Vars
 
 void Start () {
 
     boost = false;
     gotBoost = false;
     boostTimer = 0.0F;
 
     // Other Speed add ACcel Assignment
 }
 
 void FixedUpdate () {
 
     RaycastHit boostHit;
     Vector3 bottom = transform.TransformDirection (Vector3.down);
 
     if (Physics.Raycast (transform.position, bottom, out boostHit)) {
 
         if (boostHit.collider.tag == "Boost")
         {
             if(!gotBoost)
             {
                 gotBoost = true;     
                 GetComponent<AudioSource>().PlayOneShot (boostSound);
                 GameManager.Instance.SetCheckedBoost (GameManager.Instance.GetCheckedBoost () + 1); // Update from the GameManager the count of the taken boosts
                 Debug.Log ("Boosts: " + GameManager.Instance.GetCheckedBoost ()); // Print in console the taken boosts
                     
                 if (boostTimer == 0.0F) 
                 {
                         boost = true;
                         maxSpeed = GameManager.Instance.GetBoost (); // Get from the GameManager the current ship’s max speed
                         accel = 20.0F;
                 }
             }
             else {
                 gotBoost = false;
             }
 
         }
     }
 
     if (boost)
     {
         boostTimer += Time.deltaTime;
         if(boostTimer >= 5.0f)
         {
             boost = false;
             boostTimer = 0.0F;
                 
             maxSpeed = defaultMaxSpeed;
             accel = defaultAccel;
             decel = defaultDecel;
         }
     }
 }
See ya soon!
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                