- Home /
Check if another object exist in a position before respawning
Greetings!
Short problem description: I have an object that i'd like to respawn on my gaming board but do so in an "empty" place
Detailed description of my setup: I have a 2d chessboard like background (z=0 always), in this prototype the motion is done through scripting and the position x,y are always integers. I have three object type: Player (which is a cube atm of scale 1) , Many Obstacles (of scale 1) and a Fruit to pickup. Each of the object occupy a cell in my gaming board.
When the player enter the same cell as the fruit stuff happens and the Fruit respawn elsewhere on the board. What i'd like to do is having the fruit respawn in an empty cell not occupied by the player and by all the other obstacles.
Obstacles and Players have cube collider "is trigger" and rigid bodies.
In the Player Script i have
void OnTriggerEnter(Collider otherObject) {
if (otherObject.gameObject.tag == "Food")
{
//Debug.Log("We hit: " + otherObject.name);
Food food = (Food)otherObject.gameObject.GetComponent("Food");
food.ResetPosition();
}
}
and the function to reset position in the Food is
public void ResetPosition()
{
transform.position = new Vector3(Mathf.RoundToInt(Random.Range(-gameref.backx, gameref.backx)), Mathf.RoundToInt(Random.Range(-gameref.backy, gameref.backy)), gameref.transform.position.z);
}
What i'd like to add is do a check that the coordinate i'm resetting the position are "Empty", i already tried to play around with a bruteforce approach like putting an ontriggerstay and reset the position again but this doesnt seem to work for me. Any help is appreciated :) as you can see i'm using c#
Thank you! Fabio
Answer by Albert · Oct 04, 2010 at 10:41 AM
since the positions are just integers, i think it isnt hard to compare positions, you can tag all objects that moves inside the chessboard, and compare all those objects to the current moving piece or the fruit you're talking about before instantiating it.
maybe something like this might work:
if(checkIfPosEmpty(targetPosition)) { Instantiate(fruit, targetPosition, Quaternion.identity); }
public bool checkIfPosEmpty(Vector3 targetPos) { GameObject[] allMovableThings = GameObject.FindGameObjectsWithTag("movable"); foreach(GameObject current in allMovableThing) { if(current.position == targetPos) return false; } return true; }
Clave you seem to be my personal trainer =) I will try to adapt this to my code and see if it works =) The first doubt that comes to $$anonymous$$d is if i can attach multiple tags to my objects (thus adding your movable tag) since i need to avoid both players and obstacles which are two different things.
if you have multiple tags, maybe you could make two GameObject arrays and two foreach, if the Obstacles are static per game session, you could save their position in an array too on the start of the game, so you would never need to FindGameObjectWithTag on them again and again :)
Your answer
Follow this Question
Related Questions
Get the colliders transform (?) 1 Answer
How Do I Create Elastic Ropes For a Wrestling Ring? 1 Answer
My Y position goes fixed to some value 0 Answers
A node in a childnode? 1 Answer
Destory/Collect object to open door 1 Answer