using UnityEngine;
using System.Collections;
public class zumSelectObjects : MonoBehaviour
{
Vector3 mouseStartPos, mouseEndPos;
bool hasStarted, hasEnded;
Vector3 selectPointStart;
Vector3 selectPointEnd;
public Transform target;
void Start() {}
void Update()
{
if (Time.realtimeSinceStartup > 5.0f)
{
if (Input.GetMouseButtonDown(0))
{
mouseStartPos = Input.mousePosition;
Debug.Log("Start X" + mouseStartPos.x +" : Start Y" + mouseStartPos.y);
hasStarted = true;
}
if (Input.GetMouseButtonUp(0))
{
mouseEndPos = Input.mousePosition;
Debug.Log("End X" + mouseEndPos.x+ " : End Y" + mouseEndPos.y);
hasStarted = false;
hasEnded = true;
}
if (!hasStarted hasEnded)
{
selectPointStart = camera.ScreenToWorldPoint(new Vector3(mouseStartPos.x, mouseStartPos.y,1.0f));
selectPointEnd = camera.ScreenToWorldPoint(new Vector3(mouseEndPos.x, mouseEndPos.y,1.0f));
}
}
}
}
Ok, so what I have at the moment, is 2 vector 3’s one with the starting point where I clicked on the mouse down, one is the ending point where I let go of the mouse and adding a depth of 1.0f distance (1 meter), I have been trying to figure out how to then take all of that information and draw a cube starting at the start position and ending at the end position, but the only thing I can find in the docs is a cube from center point and size, ok, well I can deduce all that really from the math of the 2 vector points and have the orientation the same as the camera, but I can’t get the box to actually be created, I have written this thing over and over many times, I even tried to do a GUI based on the x and y but that gets all goofy and doesn’t start the GUI in the upper left and stretch to lower right.
This is driving me nuts, I figured this was going to be easy
Edit:
To further confuse myself, I added a gizmo to see where on earth it was thinking the point is, so add this to the code above (FYI I attached that code to the main camera)
void OnDrawGizmos()
{
if (!hasStarted hasEnded)
{
Vector3 p = camera.ScreenToWorldPoint(new Vector3(selectPointStart.x, selectPointStart.y,camera.nearClipPlane+20.0f));
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(p, 10.1f);
}
}
No matter what the x,y,z of the start point is, the gizmo always draws the sphere in the lower left at 0,0,0 with a radius of 10.1f, so it isn’t retrieving the value of the selectPointStart for some reason, but it has a value.