- Home /
Determine length of Debug.DrawRay ray
So I am running into some issues with seeing an extremely short line when I am drawing a ray using debug.DrawRay. I'm trying to setup a visible rayCast gizmo to get a visualization of the control ray I am using to move my character. Here's the code I am using to do this.
bool interactionRay (Vector3 location) {
Ray ray = Camera.main.ScreenPointToRay(location);
Debug.DrawRay(ray.origin, ray.direction, Color.green);
return Physics.Raycast(ray, out hit);
}
I was under the impression that a ray was supposed to cast for an infinite direction until it hits something, however in the editor this is what I am seeing.
The raycast is obviously working since the character is pathing to it's strike point however the debug ray is not following the same path. It's not even reaching down into a place where it's visible by the game camera. Any idea why this might happen? Have I misunderstood how the Debug ray drawing gizmo works? I don't see any option in the ray object itself to set it's length.
Answer by Eric5h5 · Jan 13, 2013 at 08:48 AM
The direction in DrawRay has a length. In Raycast, the distance is Mathf.Infinity by default. So your DrawRay and Raycast are different; you could use ray.direction*Mathf.Infinity in DrawRay to make them the same.
$$anonymous$$ultiplying the ray distance by infinity seems to cause the ray to not display at all.
Debug.DrawRay(ray.origin, ray.direction * $$anonymous$$athf.Infinity, Color.green);
I've tried caching the ray in a new var first as well. Same result.
It causes the ray to disappear for me as well in Unity 5.5, same as @darthbator addressed. Not sure whether it is a version based issue or a supposed behavior.
What Eric ment was that it would be logically the same. However the value infinity in most cases cause problems when used in a coordinate system. For example even the camera has a far clipping plane and would show anything beyond that value. If you want to visualize a ray with infinite length you may only use a "large enough" distance value:
Debug.DrawRay(ray.origin, ray.direction * 10000f, Color.green);
To add some more background:
The direction is a Vector3. When you multiply a vector with a single value each component is simply multiplied by that value. When using infinity, there are two main problems:
multiplying any non-zero value by +infinity it will result in + or - infinity depending on the sign of the value. That would effectively remove any direction information since there is nothing like "half infinity". So a vector like (0.3, -0.2, 0.93) would become (+inf, -inf, +inf).
multiplying zero with infinity will result in a NaN (Not a Number) value. That's because mathematically multiplying 0 with infinity is undefined as it could result in any possible value.
Because of those two problems it's impossible to use those values in further calculations.
The "distance" limitation value of the Raycast method is not involved in any direct computation but is simply used in a distance check comparison. So if the resulting distance is greater than that limit, the function simply returns false. If the limit is infinity, no hit distance can be larger so any hit along the ray will count.
Although I sure solved the problem by the 10000f way, the insight of infinity manipulation will probably save my future days. From what you said I conclude that the rule is, one use $$anonymous$$athf.Infinity in calculation only when he well knows what the mechanic and math principles behind. Thanks a lot sir Bunny!
Answer by Taziar · Jan 24, 2014 at 07:49 PM
You can set the distance that the Debug Ray will draw.
Debug.DrawRay(ray.origin, ray.direction * raycastDistance, Color.red, duration);
You simply multiply the Direction by a distance (float). I use the same distance variable I use to cast the actual ray and it works beautifully.
Answer by Chronos-L · Jan 13, 2013 at 06:19 AM
If the yellow line is your correct raycast and the green line is your debug ray, then I'm sure that:
1. You are drawing the Camera.main.ScreenPointToRay()'s ray, not your Physics.Raycast()'s ray. (That's why they are in a different location)
2. By this Debug.DrawRay script ref, in your code, the Debug.DrawRay()
will draw for only one frame because the duration parameter is 0 by default.
So when I do a Physics.Raycast does it not simply cast the ray that I pass in there? I was thinking of it very much as a bullet->gun analogy. The ray holds the trajectory and origin information and then the Raycast fires it out into the game environment.
The yellow line in the frame is the path the actor is taking from their current location to the destination (which is the hit.point of the Physics.Raycast I am returning from this function).
The issue I am really having is how short the debug ray is. If I increase the duration it does indeed draw longer. Then I just end up with a whole bunch of rays as I move my finger, They are no longer, they just persist. I only want the ray being drawn in frames where the finger is down on the device, so having it draw for a single frame is desired behavior.
Ok, I think I miss-phrase my number 1. Yes, you are right for your concept of Physics.Raycast().
But I do not know why there are multiple rays when you move your finger around in game, I don't think that is the default behavior for Debug.DrawRay().
I get multiple rays because the function is getting called a bunch of times as I touch and move my finger around. So if I cast the debug ray for more then 1 frame that's why I see multiples.
How about tracking an empty object to your finger, and then draw from that empty object, you should get just 1 single ray.
Yeah I actually started drawing a linecast from the ray origin to the hit location. It's functional I guess but I don't understand why the Debug.Raycast function is behaving as it is. I don't see why that ray would abruptly cut off like that.... A ray extends infinitely unless you decide to truncate it with the raycast correct? The only fields I seem to see in rays are an origin and a direction leading me to believe a ray has no inherent distance.
Your answer
Follow this Question
Related Questions
Raycast feelers not reading in all four directions. 0 Answers
Debug.draw rect 2 Answers
Draw raycast in final build 2 Answers
Debug.DrawRay not drawing since upgrading my project to 3.3.* 1 Answer