Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by areFranz · Jul 19, 2015 at 06:13 AM · c#gameobjectraycastrayhit

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...

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Priyanshu · Jul 19, 2015 at 07:18 AM 0
Share

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

avatar image areFranz · Jul 19, 2015 at 11:59 AM 0
Share

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.

avatar image barbe63 · Jul 19, 2015 at 01:13 PM 0
Share

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;

avatar image areFranz · Jul 19, 2015 at 01:38 PM 0
Share

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)

avatar image barbe63 · Jul 19, 2015 at 02:36 PM 0
Share

But you already have the colliders! Never$$anonymous$$d, glad we can help!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I make it know what it's hitting? 3 Answers

How to store player model and then swap the model to an object I Raycast hit? 0 Answers

Fall collision force with help of raycast 0 Answers

Reverse object position order 1 Answer

Raycast isn't working on small distances 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges