Prevent spawning if the gameobject has same position as a list of Vector3s using another list?
I'm stuck on what to implement in my code to prevent the spawned game object to only spawn if my buildingList vectors3's positions don't match gridTakenlist Vectors. illiterate through the array doesn't seem to work, I want to avoid using colliders (colliders will solve the issue but I want to avoid using them for this project). Feeling frustrated, but I feel I'm close to the solution.
private Vector3 rawPos;
public GameObject mainObjectSpawner;
private GameObject parentForBuildings;
public GameObject lastGameObject;
public Vector3 roundedPos;
public List <Vector3> gridTakenList;
public List<GameObject> buildingsList;
void Start () {
parentForBuildings = GameObject.Find("buildingsParent");
if (!parentForBuildings) // if parentForBuildings doesn't exist, make it exist!
{
Debug.LogWarning("No parent for Buildings found! Creating one!");
parentForBuildings = new GameObject("buildingsParent");
}
}
void OnMouseDown() {
//rawPos gets set in this method:
mousePosRay();
roundedPos = snapToGrid(rawPos);
Debug.Log("Snap To Grid Vector: " + roundedPos);
if(gridTakenList.Count <= 1)
{
Debug.LogWarning("Grid spot is open, placing building in position: " + roundedPos);
buildingSpawner(roundedPos);
buildingsList.Add((lastGameObject));
gridTakenList = new List <Vector3>( new Vector3 [buildingsList.Count]);
preventSameBuildingPosition();
}
}
void mousePosRay() {
RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hitInfo))
{
//Debug.Log("The hit point is:" + hitInfo.point);
rawPos = hitInfo.point;
}
}
Vector3 snapToGrid(Vector3 rawWorldPos) {
float newX = Mathf.RoundToInt(rawWorldPos.x);
float newY = Mathf.RoundToInt(rawWorldPos.y);
float newZ = Mathf.RoundToInt(rawWorldPos.z);
return new Vector3(newX, newY += 0.50f, newZ);
}
void preventSameBuildingPosition() {
for (int i = 0; i < buildingsList.Count; i++)
{
Vector3 vectorposition = buildingsList[i].transform.position;
gridTakenList[i] = vectorposition;
}
}
void buildingSpawner(Vector3 pos) {
// the pos vector gets passed in as roundedPos
// parentTransform gets passed in as
GameObject tempObject = Instantiate(mainObjectSpawner, pos, transform.rotation) as GameObject;
tempObject.transform.parent = parentForBuildings.transform;
lastGameObject = tempObject;
}
Answer by Maska77 · Feb 26, 2019 at 07:55 PM
Solved! 10 minutes after I posted I replaced OnMouseDown method with this code instead:
void OnMouseDown() {
mousePosRay();
roundedPos = snapToGrid(rawPos);
Debug.Log("Snap To Grid Vector: " + roundedPos);
if(gridTakenList.Contains(roundedPos))
{
Debug.Log("Spawn nothing!");
}
else
{
Debug.LogWarning("Grid spot is open, placing building in position: " + roundedPos);
buildingSpawner(roundedPos);
buildingsList.Add((lastGameObject));
gridTakenList = new List<Vector3>(new Vector3[buildingsList.Count]);
preventSameBuildingPosition();
Debug.Log("Spawn");
}
}
Your answer
Follow this Question
Related Questions
Trouble with Physics2D.OverlapArea detection 2 Answers
Shooting a bullet that stops in place when mouse is released 0 Answers
Instantiate working in Awake but on in Update 1 Answer
Object list is null? 1 Answer
How to Instantiate objects at random positions with a certain distance between each other? 2d 0 Answers