- Home /
How can I draw a specific distance with Debug.Drawray
I have this issue:
I'm using Physics2D.Raycast as next:
RaycastHit2D hit_der = Physics2D.Raycast (transform.position+new Vector3(transform.localScale.x/2,0,0),Vector3.right,1,10);
RaycastHit2D hit_izq = Physics2D.Raycast (transform.position-new Vector3(transform.localScale.x/2,0,0),Vector3.left,1,10);
Where supposedly I have my distance of 1 and it'll be true if I hit my layer 10.
Well, until here there is no problem, but now I want to see the rey with the next:
Debug.DrawRay(transform.position+new Vector3(transform.localScale.x/2,0,0),Vector3.right);
Debug.DrawRay(transform.position-new Vector3(0,transform.localScale.y/2,0),Vector3.left);
The problem here is that DrawRay doesn't allow a distance parameter, how can I draw the distance declared in Raycast using Drawray?
I've observed that if I multiply the parameter direction of Drawray, a bigger line is drawn, If I let just the direction without multipliers, is the same as my "1" distance in Raycast?
Thank you.
Answer by Glurth · Mar 14, 2015 at 02:53 PM
Rather that use DrawRay, use DrawLine, which allows you to specify both endpoints. http://docs.unity3d.com/ScriptReference/Debug.DrawLine.html
It looks like you already know how to get both the end points you need, from the ray, is that correct?
If not it would be-
end point: (ray.direction.normalized*line_length) + ray.origin
start point: ray.origin
(not sure if you actually need to normalize direction, it SHOULD be normalized already)
Ohhhhh thank you, I'd not used Drawline, yes, I have my start and end point.
Answer by abhijit93 · Aug 09, 2017 at 09:50 PM
A simple tweak to the 2nd parameter of the Debug.DrawRay would do.
Incase your Debug.DrawRay was
Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay(transform.position, forward, Color.green);
Just change it to
Debug.DrawRay(transform.position, forward * 1000f, Color.green);
Vector3 forward = transform.TransformDirection(Vector3.forward)
Check out: https://docs.unity3d.com/ScriptReference/Transform-forward.html
Answer by SteenPetersen · Aug 16, 2018 at 08:46 AM
If you are drawing a ray say like this:
RaycastHit2D hit = Physics2D.Raycast(beginRayCast, dir, myDistance, myMask);
Now you wish to visualize that ray as a debug.drawRay, you can simply do the following:
Debug.DrawRay(beginRayCast, dir.normalized * MyDistance, Color.blue, 5f);
By normalizing the direction you can now multiply it by your distance to get the same ray.
hope this helps.
Answer by Saki_col · Nov 08, 2018 at 02:36 PM
Fisrt we need to know what the DrawRay function expects from parameters
It wants the origin point, the direction and the distance of the ray (you can also give it a color, and other parameters).
public static void DrawRay(Vector3 start, Vector3 dir, Color color = Color.white, float duration = 0.0f, bool depthTest = true);
So now we need to know the direction between our ray origin and our ray.point position, to find we can use a substraction...
If one point in space is subtracted from another, then the result is a vector that “points” from one object to the other:
// Gets a vector that points from the player's position to the target's.
var heading = hit.point - ray.origin;
Now with this info we can get the direction and the distance of our ray
var distance = heading.magnitude;
var direction = heading / distance; // This is now the normalized direction.
And this will be the resulting code...
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// Gets a vector that points from the player's position to the target's.
var heading = hit.point - ray.origin;
var distance = heading.magnitude;
var direction = heading / distance; // This is now the normalized direction.
Debug.DrawRay(ray.origin, direction * distance, Color.red);
}
https://docs.unity3d.com/Manual/DirectionDistanceFromOneObjectToAnother.html https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html
Your answer
Follow this Question
Related Questions
Why doesn't the distance in RaycastAll and DrawRay correspond? 1 Answer
Get the distance between the origin and HitPoint for Raycast? 1 Answer
Is Raycast relatived to screen resolution? 1 Answer
Math - calculate position in world space from ray on infinite plane 2 Answers
Picking up rigidbody objects 2 Answers