Draw a line between point and mouse

Hi guys!

I’ve been trying to use line renderer tool to draw a line between a guiTexture on the screen and my mouse - however I just can’t seem to make it work.

Whatever I do I seem to get the coordinates wrong, and I’ve tried using Camera.main.ScreenToWorldPoint(Input.mouse) with absolutely no success…

I really need this to work :open_mouth: to make it more understandable what I want then think of the slingshot effect in angry birds. I have to throw an object which the same mechanic, and the mechanic works - just not the look. So pressing the screen will set an anchor, and your mouse will the draw a line from the anchor to its position until you release and fire…

Anyone who has some suggestions?
I was hoping to use the line renderer as it could be made pretty and all :wink:

Thanks in advance

I’ve just tested this out in XY plane (z == 0).
Here’s my code.

using UnityEngine;

public class DrawLine : MonoBehaviour
    private LineRenderer _lineRenderer;
	public void Start()
	    _lineRenderer = gameObject.AddComponent<LineRenderer>();
        _lineRenderer.SetWidth(0.2f, 0.2f);
        _lineRenderer.enabled = false;

    private Vector3 _initialPosition;
    private Vector3 _currentPosition;
    public void Update()
        if (Input.GetMouseButtonDown(0))
            _initialPosition = GetCurrentMousePosition().GetValueOrDefault();
            _lineRenderer.SetPosition(0, _initialPosition);
            _lineRenderer.enabled = true;
        else if (Input.GetMouseButton(0))
            _currentPosition = GetCurrentMousePosition().GetValueOrDefault();
            _lineRenderer.SetPosition(1, _currentPosition);

        else if (Input.GetMouseButtonUp(0))
            _lineRenderer.enabled = false;
            var releasePosition = GetCurrentMousePosition().GetValueOrDefault();
            var direction = releasePosition - _initialPosition;
            Debug.Log("Process direction " + direction);

    private Vector3? GetCurrentMousePosition()
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        var plane = new Plane(Vector3.forward, Vector3.zero);

        float rayDistance;
        if (plane.Raycast(ray, out rayDistance))
            return ray.GetPoint(rayDistance);

        return null;

Camera.main.ScreenToWorldPoint(Input.mousePosition) //This is where your problem is

The z component of the vector passed into ScreenToWorldPoint is used to determine the distance from the camera. If you just pass the Input.MousePosition it’s a Vector2 that gets cast to a Vector3 with z = 0;

Which means, the world point that is 0 distance from the camera… which will always be the camera’s position.

So… assuming that your scene is in the XY Plane at Z = 0 and that your camera is at the default depth of Z = -10.

Try this instead.

Vector3 screenPoint = Input.mousePosition;
screenPoint.z = 10;
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(screenPoint) ;

Just use a Line Renderer component on an empty object. Make that empty object follow the mouse in the game and render the line from the mouse to where you want.

Thanks for all the responses :slight_smile: I will try out these solutions today, and report back if I make it work and how for the sake of good karma towards others :slight_smile: