8
\$\begingroup\$

I want to create a line of certain length and direction.

What I mean by this is drawing a line from Point A to Point B. Normally in Unity C# to draw a line from Point A to Point B is simply:

Debug.DrawLine(point_A, point_B);

The diagram would be something like this

A----------------B

But I dont want something like this, I want it to draw from Point A to Point B but with a certain length. So I am guessing I need the direction for this to work. But I don't know how to code it.

Here is a basic diagram of how i want it.

A--------        B
\$\endgroup\$

2 Answers 2

10
\$\begingroup\$

You can simply get the vector pointing in the right direction, then scale it by the distance you want and add that to the initial point. This will define a new end point for your line.

//Get the direction of the line
Vector3 direction = point_B - point_A;
//Get a new point at your distance from point A
Vector3 point_C = point_A + (direction.normalized * distance);
//Draw the line
Debug.DrawLine(point_A, point_C);
\$\endgroup\$
2
  • \$\begingroup\$ The answer is correct, apart from one tiny detail: normalize does not return a vector in unity c# so you have to call it on direction in an extra line of code. See docs.unity3d.com/ScriptReference/Vector3.Normalize.html \$\endgroup\$
    – Kronos
    Commented Dec 16, 2014 at 14:05
  • \$\begingroup\$ @Kronos Thanks, I wasn't looking at the documentation when writing the answer, what I intended for was the normalized variable. Which essentially returns a normalized copy. \$\endgroup\$
    – House
    Commented Dec 16, 2014 at 15:05
2
\$\begingroup\$

If you know the start and end points as well as the desired length, you can use Vector3.Lerp

Vector3 midPoint = Vector3.Lerp(startVector, endVector, 0.5f);

Debug.DrawLine(startVector, midPoint);
\$\endgroup\$
3
  • \$\begingroup\$ This only works if the desired length is some known percentage of the original distance. So the code should include the calculation to determine the length ratio (between original and desired), since op didn't say it was always half way. \$\endgroup\$
    – House
    Commented Dec 16, 2014 at 14:47
  • \$\begingroup\$ I was unaware that Vector3.Lerp clamps the distance between 0.0 and 1.0. docs.unity3d.com/ScriptReference/Vector3.Lerp.html \$\endgroup\$
    – Zack Brown
    Commented Dec 20, 2014 at 11:29
  • 1
    \$\begingroup\$ It's not only clamped, but you have to calculate more than your answer shows. Say you wanted your line to be 8 units long. You'd have to first calculate the distance between your two points, which for example turns out to be 10 units, then you'd provide the lerp method with your 8/10 ratio to get the line length you want. Your answer right now only allows for lines that are as long or shorter than the distance between your two points, and it assumes that distance is already known (by using .5 in lerp). \$\endgroup\$
    – House
    Commented Dec 20, 2014 at 16:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .