- Home /
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);
Answer by Bunny83 · Jun 07, 2016 at 02:55 PM
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.
Thank you! I now have:
dir = (_B.transform.position - transform.position) * 0.5f
All I need to do now is to offset it a little because right now, it is rendered under the Blue line - I would like to shift is a pixel or two to the Right of the point.
I currently have:
Vector3 dir = (_B.transform.position - transform.position) * 0.5f;
Vector3 a_offset = new Vector3(transform.position.x + 0.1f, transform.position.y, transform.position.z);
Vector3 b_offset = new Vector3(dir.x, dir.y, dir.z);
Gizmos.DrawRay(a_offset, b_offset);
However, this is World, so it looks fine when I move _B directly above _A on the Z axis, but not when I rotate _B around _A.
Well, you need to calculate an offset that is perpendicular to your original direction. It's not clear if your problem is actually a 2D or 3D problem. 2D makes it a bit easier. In 3D it depends on the position of the camera.
In 2D you can simply swap the x and y coordinates of your direction and invert one coordinate to get a vector that is perpendicular on the original.
Vector2 offset = new Vector2(dir.y, -dir.x).normalized * 0.1f;
This offset can simply be added to your origin.
If it should work in 3D you have to calculate the cross product between your direction vector and your camera's forward vector. That gives you a vector that is perpendicular to the plane that your direction and the forward axis define.
Something like this:
Vector3 offset = Vector3.Cross(dir, Camera.current.transform.forward).normalized * 0.1f;
Your answer
Follow this Question
Related Questions
Shoot second cell in 2d 0 Answers
CharacterController.Move Issue 2 Answers
Rotate the player toward a direction 0 Answers
InverseTransformDirection and TransformDirection 1 Answer
tranform.TransformDirection not effecting spawned object 2 Answers