I know there are an abundance of resources on 2d selections boxes, and I've used them in the past.
For my game, I think a 3d selection box would be a lot more accurate. Here's an example of what I mean (look at 1:00-1:10) :
https://www.youtube.com/watch?v=BiBrsA2cBLo
At the moment, my game is completely flat so I don't need to worry about height differences just yet (so I guess it's still technically a 2d selection box!). I'm using a strict grid for building placement, object placement, and pathfinding. The center of each grid cell is a whole vector3 (i.e. 5, 0, 10) and the size of each cell is 1 Unity meter squared.
Here's a script I made that instantiates a cube on grid cells according to mouse movement. Lame, but it's a start. The cube or 'selectionTarget- contains a script that sends information about the types of objects it's colliding with.
using UnityEngine;
using System.Collections;
public class SelectionManager : MonoBehaviour
{
//vector3
public Vector3 roundedMousePosition;
//gameObject
public GameObject selectionTarget;
private void Update()
{
SelectLocation();
}
private void SelectLocation()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity))
{
selectionTarget.SetActive(true);
int x = Mathf.FloorToInt(hitInfo.point.x);
int z = Mathf.FloorToInt(hitInfo.point.z);
roundedMousePosition.x = x;
roundedMousePosition.z = z;
roundedMousePosition.y = 0;
selectionTarget.transform.position = roundedMousePosition;
}
}
}
I need to find a way to click and drag that cube so it can encompass more than one cell at once.... otherwise clearing land will be a very tedious process!!!!
↧