Hello Unity,
I am looking for some help to draw a box when I click down on the mouse. Here is what I have so far and its not doing anything at all. I think once I get this working I am still going to have the issue that the box wont draw until after I let up on the mouse.
Does anyone know of any good wiki's or anything that cover using the GUI to draw shapes?
Here is what I have so far.. The idea is that the size of the cube is calculated by the distance the mouse is from the first point when you clicked. That way the cube will get bigger as you move the mouse farther away.
public class DrawLine : MonoBehaviour {
private Vector3 startPos;
private Vector2 cursorPoint;
private float mouseDistanceFromStartPoint;
private bool iAmHoldingMouseDown;
private float cubeSize;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(iAmHoldingMouseDown)
{
cubeSize = Vector3.Distance(Input.mousePosition, startPos);
}
}
void OnDrawGizmos()
{
Debug.Log("Drawing line");
Gizmos.color = Color.black;
Gizmos.DrawCube(startPos, new Vector3(cubeSize, cubeSize, 0));
}
void OnMouseDown()
{
Debug.Log("Mouse Down");
iAmHoldingMouseDown = true;
startPos = Input.mousePosition;
}
void OnMouseUp()
{
Debug.Log("Mouse Up");
iAmHoldingMouseDown = false;
}
}
For the record the mouse up and down debugs are running, as I have a box as the background which has a collider.
↧