Gizmos.DrawRay is Incorrect

Hello,

I’m trying to draw a ray within my Gizmo, but I can’t seem to get it to go in the correct direction.

I basically have two GameObjects, called A and B. A is connected to B, and fires a Blue DrawLine from A to B. I want to also fire a smaller Green line from A, in the DIRECTION of B, but for it to only be a short line, and also offset to the left. This is to show the user that A is connected to B.

This is what I currently have:

The Upper Left is B, the Lower Right is A. A is connected to B (shown by the Blue Line).

I want to Green Line to be basically 180 degrees the other way, and offset a little so its not on-top of the Blue Line.

This is what I want:

How can I achieve this?

//this transform is _A
Gizmos.color = Color.green;
Vector3 dir = transform.TransformDirection(transform.position - _B.transform.position) * 0.5f;
Gizmos.DrawRay(transform.position, dir);

Gizmos.color = Color.light_blue;
Gizmos.DrawLine(transform.position, _B.transform.position);

The rule for vector math is: “tip minus tail”. So to get a direction vector that goes from A to B you need to calculate (B - A). You calculate A - B at the moment, so the vector points the other way round.

ps: Your TransformDirection call is most likely wrong as well. transform.position is a position in worldspace. So the resulting direction vector after you calculated (B - A) is also a world space direction. TransformDirection transforms a local space vector into worldspace. So you should remove the TransformDirection completely here.