- Home /
Draw an Arrow on the Screen
Hi,
I would like to draw an arrow on the screen, from one coordinate to another on the screen, so the coordinated are the screen coordinates (2D) rather that in the 3D world. This will change depending on where the player's finger is on the screen.
How can I make an arrow appear from one coordinate to another, preferably "dynamic", so the arrow can be longer without it being stretched?
Thanks!
Vectrosity would be perfect for that; it would only take a few lines of code.
Answer by robertbu · Jul 14, 2013 at 03:45 PM
Here is a script that will position, rotate, and scale an object to extend between two game objects dynamically. Its current form uses the 'y' of the object, so the top and bottom are at the two specified positions. try it with build-in cylinder, built-in cube, or a 1 unit, vertical plane from the CreatePlane editor script. It only scales the 'Y' values, so the x, and z scaling can be set as appropriate for visual look. In order for the arrow to not look stretched, make the arrow head a separate object. Place it at the end point, rotate it, but don't scale it.
pragma strict
var pos1 : Vector3;
var pos2 : Vector3;
var objectHeight = 2.0; // 2.0 for a cylinder, 1.0 for a cube
function Update () {
if (Input.GetMouseButtonDown(0)) {
pos1 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
pos1 = Camera.main.ScreenToWorldPoint(pos1);
pos2 = pos1;
}
if (Input.GetMouseButton(0)) {
pos2 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
pos2 = Camera.main.ScreenToWorldPoint(pos2);
}
if (pos2 != pos1) {
var v3 = pos2 - pos1;
transform.position = pos1 + (v3) / 2.0;
transform.localScale.y = v3.magnitude/2.0;
transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
}
}
How can I make this so its between 2 points on the screen, ie not in the 3D space, but in the 2D GUI space?
So Like from (500,100) to (300,200) for example.
and Should it be function OnGUI()
rather than function Update()
?
As @Eric5h5 mentions, Vectrosity is an excellent line drawing package you can find in the Asset store that makes this problem much easier. Note the code above should be in Update(). If you wanted to draw 2D using the code above and using mouse positions, then move the two positions used in the code above to the mouse position just beyond the near clip plane of the camera. That can be accomplished using Camera.ScreenToWorldPoint().
var v3Pos = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.1);
v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
I've edited the code above so that you can drag out the object using the mouse.
The reedited code above was tested with a cylinder scaled to (0.005, 1.0, 0.005).
Your answer
![](https://koobas.hobune.stream/wayback/20220613110059im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Display a Line in GUI without texture - GUI Line ? 6 Answers
Free Gui solutions 2 Answers
Unity 4.7 - OnGUI prevent click/touch through 0 Answers
OnGUI button created by a foreach loop 4 Answers
how to render line between gui element and mouse position 1 Answer