Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by jessee03 · Jan 21, 2013 at 02:23 AM · raycast

Stopping a raycast after hit

I noticed that my raycasts have been hitting multiple objects. Is there a way to stop a raycast after it hits an object? I've searched around but haven't been able to find anything about it. I believe that Mathf.Infinity needs to be changed to something else.

Comment
Add comment · Show 2
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 sparkzbarca · Jan 21, 2013 at 03:23 AM 0
Share

Yea that isn't what your wanting, what are you wanting?

Information on the first target hit?

thats in the out raycasthit variable.

You need to say this is what i'm trying to do, this is what i want kind of thing.

I know you think you want the raycast to stop but my guess is that isn't the problem, thats a possible solution to the problem, tell us your actual problem. We will give you the actual solution (and thats not it).

avatar image jessee03 · Jan 21, 2013 at 03:32 AM 0
Share

when I call my raycast it keeps going forever hitting every object in it's path. I want it to stop after hitting a single object.

3 Replies

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

Answer by devstudents · Oct 31, 2014 at 12:20 PM

jessee03, I think your question is very clear. I had exactly the same problem. I thought the raycast was shooting through two objects and, like you, I thought it had something to do with Mathf.Infinity.

You've probably solved it by now but here was what I was doing wrong:

     void Update() 
     {
         RaycastHit hitPoint;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
         if(Physics.Raycast(ray, out hitPoint, Mathf.Infinity))
         {
 
             if(Physics.Raycast(ray, out hitPoint, Mathf.Infinity) == GameObject.FindGameObjectWithTag("Ground"))
             {
                 Debug.Log("Hit ground"); 
             }
             
             if(Physics.Raycast(ray, out hitPoint, Mathf.Infinity) == GameObject.FindGameObjectWithTag("Object"))
             {
                 Debug.Log("Hit object"); 
             }
         }
         else
         {
             Debug.Log ("No collider hit"); 
         }
 
     }

And this is what worked:

 void Update() 
     {
         RaycastHit hitPoint;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
         if(Physics.Raycast(ray, out hitPoint, Mathf.Infinity))
         {
             if(hitPoint.collider.tag == "Ground")
             {
                 Debug.Log("Hit ground"); 
             }
         
             if(hitPoint.collider.tag == "Object")
             {
                 Debug.Log("Hit object"); 
             }
         }
         else
         {
             Debug.Log ("No collider hit"); 
         }
     }




Comment
Add comment · Show 3 · 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
avatar image Enyph-Games · May 21, 2016 at 03:46 PM 0
Share

I love you man. ;) $$anonymous$$y head was exploding for the last few days for this, you saved my life. :D Thank you VERY much. :D

avatar image devstudents Enyph-Games · May 21, 2016 at 06:04 PM 0
Share

No worries :)

avatar image Merlok_3_0 · May 21 at 12:10 PM 0
Share

use compareTag

avatar image
3

Answer by jwinn · Jan 21, 2013 at 03:38 AM

Spark is right; if you only want information on what the raycast first hits, then just use the first returned hit point.

If you mean drawing it, you could you use Debug.DrawLine from the raycast's (Physics.Raycast) point of origin to the first hit point.

Comment
Add comment · Show 6 · 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
avatar image jessee03 · Jan 21, 2013 at 03:40 AM 0
Share

This is my code. I just want the raycast to only trigger the code for the first object it hits.

if(Input.Get$$anonymous$$ouseButtonDown(0) && collider.Raycast(Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)), hit, $$anonymous$$athf.Infinity))

avatar image sparkzbarca · Jan 21, 2013 at 03:49 AM 0
Share

see your still not telling us what you really. want your telling us you have a problem and this is the solution.

I want you to say your problem without using the word raycast.

what is this code for?

collider.Raycast ONLY CASTS AGAINST THAT SPECIFIC COLLIDER. you cannot be wanting that, because by it's very definition that is what is does. the problem is this is probably attached to a bunch of objects and you want like the first hit or something I don't know. and thats the thing I DON'T $$anonymous$$NOW WHAT YOU REALLY WANT.

use plain english, say what want. dont use raycast, dont use program$$anonymous$$g words.

Say i'd like to find the nearest enemy or something. say the end result.

avatar image jwinn · Jan 21, 2013 at 03:51 AM 0
Share

collider.Raycast "ignores all colliders except this one"; maybe you need Physics.Raycast? Check the last two examples: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

avatar image jessee03 · Jan 21, 2013 at 03:53 AM 0
Share

Whoa calm down buddy... That's not helping either of us out either. It's a pretty straight forward question. Don't use the word raycast when my main issue is about raycasts? I have multiple objects in my scene with this code attached. When the raycast hits an object it calls the code. $$anonymous$$y problem is that the raycast goes through more than one object. So if there's a group of boxes in a row I only want to be able to click on the first one and not the others.

avatar image sparkzbarca · Jan 21, 2013 at 04:13 AM 0
Share

well the raycast doesnt go through more than 1. it's as i already said, you have it attached to a bunch and you raycast a bunch of times and hit each in succession. By definition collider.raycast only ever hits 1 collider, it never hits more than one. SO thats not your problem. It is by definition impossible for that to be your problem. I understand you think that is your problem, your actual problem is partly that your calling it on each and every collider and hitting them all once. its not that one raycast is hitting 10 colliders its that 10 raycasts are hitting 10 colliders.

so you want when you click to select the first object under the crosshair (you'll notice i didnt use the word raycast ever).

if (input.mousebuttondown(0)) { physics.raycast(Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)), out hit); } hitobject = hit.gameobject;

//if there is code you want to call hitobject.function_name();

if you'd just say i'd like to click my mouse and run a function on the object under the mouse or something. Say you'd like to make it rotate, select it, anything. If you would say what you want as if you didnt know how to program, you just knew what you want the end result to be i could be more helpful

avatar image PhantomMagic sparkzbarca · Jun 20, 2016 at 11:39 PM 0
Share

in unity is their a way to cancel the raycast after you hit the object? I have projectiles falling from the sky in my game and I dont want the player to hold their finger on the screen to destroy them. I only want to give them the option to tap on the objects to destroy them.

void Update () { if (Input.touchCount > 0 || Input.GetTouch (0).phase == TouchPhase.Began) // when you touch at the start {
ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position); // creating a ray Debug.DrawRay (ray.origin, ray.direction * 20, Color.red);// visiually see where the ray is going if (Physics.Raycast (ray,out hit,$$anonymous$$athf.Infinity)) // this when oject is hit something { Destroy (hit.transform.gameObject); Debug.Log ("Hit something"); // if it hits shows on concolue } }
}

avatar image
0

Answer by NejoFTW · Dec 15, 2017 at 10:21 PM

Hi. I know it's kind of an old question, but here was my approach:

I did it with raycasting- I used RaycastAll and layers for my top down kind of moba style game, i used it for highlighting enemies and stuff. In Update you need : Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hits; hits = Physics.RaycastAll(ray);

//then in the array of hits you check if the 0 element in the array is on the layer you are looking for,let's //say shootable is on layer 9, and let's assume ground is layer8 if (hits[0].collider.gameObject.layer==9) { hoveringShootable = hits[0].collider.gameObject; hits[0].collider.gameObject.GetComponentInChildren().UserCall(true); }

//It's working for now, I hope it helps if you have the same obstacle I was facing!

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycasting vs. Input.GetMouseButtonDown 2 Answers

2D Raycast over multiple objects not working? 1 Answer

Check if RaycastHit variable is different. 3 Answers

Change Raycasr origin 0 Answers

Disabling a lineRender or Ray when there is no target 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