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.

Rather that use DrawRay, use DrawLine, which allows you to specify both endpoints.

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)

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);

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.

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