# How to draw a line in 3d space?

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

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?

• What object is that update-function attached to? Is it possible you are actually drawing the line below the plane? – Philipp Jul 29 '15 at 9:35
• 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. – Philipp Jul 29 '15 at 9:39
• The script is attached to the object WhiteBall, the white ball in the 8 ball pool game, is that ok? – mirzahat Jul 29 '15 at 10:12
• 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? – Philipp Jul 29 '15 at 10:22
• are Gismos turned on ? are you looking in debug window? – dnk drone.vs.drones Jul 29 '15 at 10:24

## 2 Answers

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

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.