0
\$\begingroup\$

I have two objects with a line going from one to the other, like this:

enter image description here

I want to create another line with a starting point one of the objects and an end point the mouse position, however still facing the second object, which will look like the first line is filling up to where the mouse is.

For example, I want to connect the blue shape with the green shape, so I move my mouse from one to the other. The game finds the nearest white line from the cursor and creates another line on top of it that follows my cursor, like this:

enter image description here

The current script is this. It draws a line from the origin shape to the mouse position, but it doesn't stay on only one direction and instead moves where my mouse goes:

public class SelectionLine : MonoBehaviour {
private LineRenderer lineRenderer;

private void Awake() {
    lineRenderer = GetComponent<LineRenderer>();
}

private void Update() {
    if (ShapeSelection.instance.selectedShape != null && ShapeSelection.instance.draggingFinger != null) {
        var originPosition = ShapeSelection.instance.selectedShape.transform.position;
        var destinationPosition = ShapeSelection.instance.draggingFinger.ScreenPosition;

        lineRenderer.SetPosition(0, new Vector2(originPosition.x, originPosition.y));
        lineRenderer.SetPosition(1, new Vector3(ShapeSelection.instance.draggingFinger.GetWorldPosition(1f).x, ShapeSelection.instance.draggingFinger.GetWorldPosition(1f).y, 0));
    }
}

}

There is no way (as far as I know) to set a direction of a line renderer in Unity, so how can I achieve this?

\$\endgroup\$
  • \$\begingroup\$ The lineRenderer will draw a line to whatever position you tell it to draw a line to. If you don't want it to draw a line to the cursor, tell it to draw a line to the position you actually want it to draw to? \$\endgroup\$ – Trevor Powell Nov 3 '16 at 0:26
  • \$\begingroup\$ @TrevorPowell The line should draw to the same position as the cursor, but only if the cursor is nearby, so it looks like it follows you through what you want to select while still staying at the same position as the white line \$\endgroup\$ – Milen Nov 3 '16 at 7:50
1
\$\begingroup\$

enter image description here

It seems to me like it is a trigonometry question. Length of B = (length of A) * cos(alpha). To get alpha you can refer to: http://answers.unity3d.com/questions/317648/angle-between-two-vectors.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.