- Home /
World space selection box
Hi,
I am trying to create an RTS selection box in world space. I want it to be aligned with the camera. I use a world space canvas. It works correctly as long as the camera isn't rotated but breaks down as soon as the camera rotates. I take it I have to rotate the points from the selection to make the size match up but can't figure it out.
void Displaybox(float angle) // The angle is the cameras current y rotation
{
selectionBoxTransform.localRotation = Quaternion.Euler(new Vector3(90f, 0f, angle));
//Get the middle position of the box
Vector3 middle = (boxStartScreen + boxEndPos) / 2f;
//Set the middle position of the GUI box
selectionBoxTransform.position = middle;
//Change the size of the box
float sizeX = Mathf.Abs(boxStartScreen.x - boxEndPos.x);
float sizeY = Mathf.Abs(boxStartScreen.z - boxEndPos.z);
//Better to use local scale?
selectionBoxTransform.sizeDelta = new Vector2(sizeX, sizeY);
}
aligned.jpg
(169.8 kB)
rotated.jpg
(103.6 kB)
Comment