- Home /
Transform.forward inaccurate.
private void OnDrawGizmos() {
Gizmos.color = Color.blue;
Gizmos.DrawLine(transform.position, transform.forward * 180);
}
It works perfectly fine on other objects. the only other mentions of the objects transform in the code are transform.Rotate() and rigidbody.AddForceAtPosition(transform.forward, position), which gives inaccurate results, because the transform.forward is not facing forward.
I am incredibly confused. Any ideas why this is happening?
Answer by Bunny83 · Oct 20, 2019 at 12:25 AM
This line just makes no sense:
Gizmos.DrawLine(transform.position, transform.forward * 180);
DrawLine expects two positions in worldspace. You passed in one position and one direction vector which you scaled by 180. So you essentially create a worldspace position that is transform.forward*180 away from the world origin.
So your line drawing is already wrong. It's not clear what you line should represent. However if you just want to draw the forward axis relative to the objects position, you have to do either
Gizmos.DrawLine(transform.position, transform.position + transform.forward * 180);
or use Gizmos.DrawRay instead which actually takes one position and one direction vector.
Gizmos.DrawRay(transform.position, transform.forward * 180);
As for your potential AddForceAtPosition issue, it's not clear what exact values you pass to the method. Keep in mind that the object's position doesn't need to be the center of mass of the object. If you apply a force at a position that is not the center of mass the object will not only move but also rotate since the force attack the object off-center.
Fixed the gizmo, but the problem persisted. miraculously the problem was gone after i restarted unity. Thanks for the answer anyway. I apologize for the horrible question.