Drawing a line from this.transform.position to input.mousePosition

Ok, i’ve got the following script:

using UnityEngine;
using System.Collections;

public class playerRayCast : MonoBehaviour {
 
    Vector3 touchPosition;

    void Start () {
        this.gameObject.AddComponent<LineRenderer> ();
        this.gameObject.GetComponent<LineRenderer> ().SetWidth (0.01f, 0.01f);
    }

    void Update () {
        if (Input.GetMouseButton (1)) {
            touchPosition = Input.mousePosition;
            this.gameObject.GetComponent<LineRenderer>().SetPosition (0, this.transform.position);
            this.gameObject.GetComponent<LineRenderer>().SetPosition (1, touchPosition);
            this.gameObject.GetComponent<LineRenderer>().SetColors (Color.white, Color.white);
        }
    }

}

The script is attached to a game object which is on the left hand side of the screen space. When I right click it generates the line but the line is NOT going to where the mouse position is…

Any ideas?

mousePosition is in “screenspace” the rest is in worldspace

look at the scripting reference for it: Unity - Scripting API: Input.mousePosition

if you don’t want to raycast you might want to look at: Unity - Scripting API: Camera.ScreenToWorldPoint

You’re a legend thankyou. I’m working to a tight deadline and I’m not in the zone today, I should of checked the API sorry for the time waste!