Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by ShottyMonsta, Jun 3, 2014.

  1. ShottyMonsta

    ShottyMonsta

    Joined:
    May 23, 2014
    Posts:
    25
    Ok, i've got the following script:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class playerRayCast : MonoBehaviour {
    5.  
    6.     Vector3 touchPosition;
    7.  
    8.     void Start () {
    9.         this.gameObject.AddComponent<LineRenderer> ();
    10.         this.gameObject.GetComponent<LineRenderer> ().SetWidth (0.01f, 0.01f);
    11.     }
    12.  
    13.     void Update () {
    14.         if (Input.GetMouseButton (1)) {
    15.             touchPosition = Input.mousePosition;
    16.             this.gameObject.GetComponent<LineRenderer>().SetPosition (0, this.transform.position);
    17.             this.gameObject.GetComponent<LineRenderer>().SetPosition (1, touchPosition);
    18.             this.gameObject.GetComponent<LineRenderer>().SetColors (Color.white, Color.white);
    19.         }
    20.     }
    21.  
    22. }


    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?
     
    Last edited: Jun 3, 2014
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. ShottyMonsta

    ShottyMonsta

    Joined:
    May 23, 2014
    Posts:
    25