- Home /
How to draw an arrow in Unity
Hello Everyone.
I am trying to create a simple Chess UI program in unity and would love if the players could draw arrows on the screen. I am currently following very closely the code In this blogpost for how to draw arrows. Unfortunately, when I run my code in unity, all I get is a few tiny pink pixels that don't go very far away from the initial mouse position no matter how much I drag. Here is the script I have on the game object "board" which I want to draw the arrows on. `
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Board : MonoBehaviour
{
public GameObject controller;
public float arrowheadSize;
private Vector3 startPosition, mouseWorld;
GameObject arrow;
private LineRenderer arrowLine = new LineRenderer();
public void Start(){
arrow = GameObject.FindGameObjectWithTag("Arrow");
arrowLine = arrow.GetComponent<LineRenderer>();
mouseWorld = new Vector3 ();
arrowheadSize = 1.2f;
}
public void OnMouseDown(){
// mostly not relevant I think. All of my current testing happens when "selecton" is off.
controller = GameObject.FindGameObjectWithTag("GameController");
if (controller.GetComponent<Game>().selecton){
controller.GetComponent<Game>().selecton = false;
controller.GetComponent<Game>().selected. GetComponent<SpriteRenderer>().size -= new Vector2(0.03f, 0.03f);
controller.GetComponent<Game>().selected. GetComponent<SpriteRenderer>().drawMode = SpriteDrawMode.Simple;
controller.GetComponent<Game>().selected. GetComponent<Chessman>().DestroyMovePlates();
} if (true) /*eventually this will be an actual conditional.
For now though I want this behavior to occur everytime the mouse is clicked*/ {
// Here seems to be the problem... somewhere
mouseWorld = Camera.main.ScreenToWorldPoint (
new Vector3 (Input.mousePosition.x,
Input.mousePosition.y,
Camera.main.nearClipPlane
));
startPosition = mouseWorld;
}
}
public void OnMouseDrag(){
//Turn on the arrow
arrowLine.enabled = true;
DrawArrow ();
}
public void DrawArrow(){
mouseWorld = Camera.main.ScreenToWorldPoint (
new Vector3 (Input.mousePosition.x,
Input.mousePosition.y,
Camera.main.nearClipPlane
));
//The longer the line gets, the smaller relative to the entire line the arrowhead should be
float percentSize = (float) (arrowheadSize / Vector3.Distance (startPosition, mouseWorld));
//h/t ShawnFeatherly (http://answers.unity.com/answers/1330338/view.html)
arrowLine.SetPosition (0, startPosition);
arrowLine.SetPosition (1, Vector3.Lerp(startPosition, mouseWorld, 0.999f - percentSize));
arrowLine.SetPosition (2, Vector3.Lerp (startPosition, mouseWorld, 1 - percentSize));
arrowLine.SetPosition (3, mouseWorld);
arrowLine.widthCurve = new AnimationCurve (
new Keyframe (0, 0.2f),
new Keyframe (0.999f - percentSize, 0.2f),
new Keyframe (1 - percentSize, 0.7f),
new Keyframe (1 - percentSize, 0.7f),
new Keyframe (1, 0f));
}
void OnMouseUp(){
//Turn off the arrow
arrowLine.enabled = false;
RaycastHit hit;
Physics.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 100);
transform.position = new Vector3(hit.point.x, transform.position.y, hit.point.z);
}
}
I believe the problem is in OnMouseDown as when I set the if(true) to if(false), meaning that startPosition is never set to mouseWorld and always remains new Vector3() the arrow works fine! It can only point away from the center of the board though.
Your answer

Follow this Question
Related Questions
Add pointer Up / Down at runtime (Button UI 4.6) 0 Answers
How to implement Start() as an Event System trigger Type? 0 Answers
Should I use EventTriggers as a component or as a superclass for another component? 0 Answers
IsPointerOverGameObject() is true for wrong object sometimes 1 Answer
fire OnSubmit when key released 1 Answer