I have a top down billiard game. And on click / tap the ball moves in the clicked direction like in the image below:

enter image description here

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);

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




I tried DrawLine, Drawray, any ideas?

  • \$\begingroup\$ What object is that update-function attached to? Is it possible you are actually drawing the line below the plane? \$\endgroup\$ – Philipp Jul 29 '15 at 9:35
  • \$\begingroup\$ By the way, a more appropriate class than Debug for drawing spatial user interfaces is the class Gizmos. Any calls to it need to be in the OnDrawGizmoz event. \$\endgroup\$ – Philipp Jul 29 '15 at 9:39
  • \$\begingroup\$ The script is attached to the object WhiteBall, the white ball in the 8 ball pool game, is that ok? \$\endgroup\$ – mirzahat Jul 29 '15 at 10:12
  • 1
    \$\begingroup\$ Another problem could be that the default color for Debug.drawLine is white, just as your background. Could you be drawing white-on-white? Also, did you check if transform.forward relative to your cueball is maybe downwards? \$\endgroup\$ – Philipp Jul 29 '15 at 10:22
  • 2
    \$\begingroup\$ are Gismos turned on ? are you looking in debug window? \$\endgroup\$ – dnk drone.vs.drones Jul 29 '15 at 10:24

Use a LineRenderer. That's a component which draws a line in 3D space.

|improve this answer|||||

Ask jhocking said you could use a LineRenderer and then apply a texture to it for the Ray to get the effect you want. LineRenderer could be easily controlled from a script.

|improve this answer|||||

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.