- Home /
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.
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).
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.
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");
}
}
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
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.
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))
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.
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
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.
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
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 } }
}
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!
Your answer
Follow this Question
Related Questions
How to obtain third person accuracy 0 Answers
How to send a raycast to an object to give me ammo? 0 Answers
raycast rigidbody addforce 2 Answers
DontGoThroughThings vs Triggers 1 Answer