- Home /
Vector3 List Find Contains Specific X Coordinate Value and Remove
Im trying to make a dynamic grid system with the mouse. Dont worry about the details too much Im just interesting in how to determine if a list of vector3 have X values that dont contain a number then remove that element. But essentially I click and drag the mouse and its like an RTS selection but rather than a 2d rectangle it places a gridobject down and updates depending on the mouse position
SO to place them down I have this:
void updateGrids(Vector3 start, Vector3 mouse)
{
if (start.x < mouse.x) {
for (int x = (int) start.x; x <= mouse.x; x++) {
if (start.z < mouse.z) {
for (int z = (int) start.z; z <= mouse.z; z++) {
if (!gridsPos.Contains(new Vector3(x, start.y, z)))
{
GameObject obj = findInactiveGrid();
obj.transform.position = new Vector3(x, start.y, z);
gridsPos.Add(new Vector3(x, start.y, z));
}
}
...... (more etc)
and to remove them if my mouse moves to a different coordinate then remove the grid objects that are not within the rectangular coordinates. So if grid does not contain the mouseRay origin.x then remove it). Never new how to use Lists until lately so Im struggling. I know this would be easier with a dictionary with Gameobjects keys and vector3 position as values but I dont know how to use it completely yet
void UpdateRemove(Vector3 start, Vector3 mouse)
{
if(!gridPos.Contains
gridsPos.con
}
So is it actually a grid based game or can objects be placed freely? 2D or 3D ? How many objects do you have on your grid on average ?
List.Contains()
loops through the list to check for matches, so it's really slow. Imagine selecting a 30x30 area with 10 units. You could end up in a situation there the 10 units are already added to the list but this code still checks the remaining 890 empty coordinate points against the 10 units in the list. That's almost 10000 checks.
Vector3 x, y and z are floats so I'd say you also risk getting false negatives with if (!gridsPos.Contains(new Vector3(x, start.y, z)))
.
I don't know what your game is like but 9 times out of 10 it makes more sense to check a list of units to see if they're inside the selection area and mark them as "selected" with a boolean and go from there.
Its like a tycoon. Its gridbased with rooms and then objects are placed freely. Its 3d spaced, think of it like sims I suppose but its themed.
I have a rounding function that selects the proper tile so rounding is not the issue. Now that I think about it, you are correct this implementation was silly (its good for placing but not searching for deleting grids) so to accommodate I was thinking about using collision testing.
As I drag the mouse, I would modify a dummy trigger box collider. Click down sets the origin and then dragging the mouse would update the verticies accordingly for the bounds. I failed doing this portion earlier. Any help on it via code?
So once thats done I would check objects if they lie within the bounds of the collider, if not then I would do List.Remove for those that do not lie within the 2d box collider. This seems more practical and efficient.
Note: TO clear up, theres no units but NPC sims. The grids are just dummy objects for the projectors to project a grid onto the map itself (:
Heres an image to get a better picture of what Im doing:
http://i.gyazo.com/855c1198d7d2e51f89af5b6e3b632017.gif
So thats working perfectly and Click and dragging works (thanks to the first function i posted updateGrids() but when I move the cursor inwards it doesnt remove the grids out of bounds relative to the mouse. I hope this clears things up
Your answer
Follow this Question
Related Questions
Checking duplicate position in a list 1 Answer
A node in a childnode? 1 Answer
instantiate problem help? 1 Answer
Vector3[]? 1 Answer
Set Specific List Value 3 Answers