How to rotate DrawLine with an object?

I have a cube and I am using Debug.DrawLine(transform.position, vector3.forward * 10, Color.red);

This is the results I get

When the cube rotates, the line does not rotate with the cube. It is always pointing in the global Z axis.

A long time ago I used LookAt function and debug.DrawLine and it was working without implementing any other code for rotating the line with the cube. I have no idea why its not working now =(

I believe you want lookAt instead of vector3.forward.

No, you’d use transform.forward. However Debug.DrawRay would be more appropriate.

–Eric

1 Like

When do you use lookAt Eric?

DrawRay works

So what is the end code? Because Debug.DrawRay(transform.position, vector3.forward, Color.red); does that same thing.

Vector3.forward is a static vector (0,0,1). You have to use MySillyCubeTransform.TransformDirection(Vector3.forward) to make it a direction relative to the cube.

These will also do the same thing:

Debug.DrawRay(transform.position, transform.forward * distance, Color.red);
Debug.DrawLine(transform.position, transform.position + (transform.forward * distance), Color.red);
1 Like