Search Unity

Drawing a Line very difficult, why?

Discussion in 'Scripting' started by mirzahat, Jul 29, 2015.

  1. mirzahat

    mirzahat

    Joined:
    Dec 21, 2014
    Posts:
    53
    I have a top down billiard game. And on click / tap the ball moves in the clicked direction like in the image below:

    https://www.dropbox.com/s/x5ktxvlefjeqf1b/img.jpg?dl=0

    Now, I want a line to be drawn in the direction where the mouse is hovers or the finger is swiped, here is my update code:

    void Update () {

    //this should make the line, why not working?
    Vector3 forward = transform.forward;
    Debug.DrawRay(transform.position, forward);
    Debug.DrawLine(transform.position, transform.position + forward * 10000);


    if (Input.GetMouseButtonDown (0)) {

    //
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if (Physics.Raycast(ray, out hit, 100.0f)){

    //


    //
    Vector3 startPos = transform.position;
    Vector3 clickPos = new Vector3(hit.point.x, hit.point.y, transform.position.z);
    Vector3 direction = Direction (clickPos - startPos);

    //direction
    rb.velocity = new Vector3(direction.x * 5, direction.y * 5, 0);


    }



    }



    }
    I tried DrawLine, Drawray, any ideas?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Debug functions only work in scene view.

    Look up "Unity Line Renderer"


    also, please use [code ][/code ] tags when pasting code into the forums, really helps with readability. There is a sticky on them at the top of the scripting forum.