- Home /
Function to cheack if there is already a gameobject in a indicated vector not working
Hello i have the following function that will retrieve a true of false statement depending if there is already a gameobject with a tag of tiles in a de-terminated location
bool CheackGameObjectPos(Vector3 Pos)
{
GameObject[] Array = GameObject.FindGameObjectsWithTag("Tiles");
foreach (GameObject Game in Array)
{
Vector3 ZeroPos = new Vector3(0, 0);
if (Pos.x == ZeroPos.x && Pos.y == ZeroPos.y)
{
return true;
}
Vector3 NewPos = new Vector3(Pos.x, Pos.y, 0);
if (Game.transform.position.x == NewPos.x && Game.transform.position.y == NewPos.y)
{
return true;
}
}
return false;
}
For some reason that i cant figure out it sometimes does not work
Answer by Pekira · Jun 12, 2017 at 09:10 AM
The problem was when a float point imprecision happen the code would say broths vectors where different even that the difference as just 0.0000001 so i add the follow code
if (Mathf.Approximately(Game.transform.position.x, NewPos.x) && Mathf.Approximately(Game.transform.position.y, NewPos.y))
{
return true;
}
This Codes uses a Mathf function that compares 2 floats and return if they are similar if yes it will return true saying that a vector already exists
So basically this is what the code looks like
bool CheackGameObjectPos(Vector3 Pos)
{
GameObject[] Array = GameObject.FindGameObjectsWithTag("Tiles");
foreach (GameObject Game in Array)
{
Vector3 ZeroPos = Vector3.zero;
if (Pos.x == ZeroPos.x && Pos.y == ZeroPos.y)
{
return true;
}
Vector3 NewPos = new Vector3(Pos.x, Pos.y, 0);
if (Mathf.Approximately(Game.transform.position.x, NewPos.x) && Mathf.Approximately(Game.transform.position.y, NewPos.y))
{
return true;
}
}
return false;
}
@ShadyProductions thank you a lot for your answer but the code was a little bit difficult to understand
Answer by ShadyProductions · Jun 11, 2017 at 09:17 AM
Please note that a position contains float values, so the position needs to be exact. If you pass in a vector that has x on 0.34 and you are comparing to one that has position 0.35 then it will return false.
I also simplified it a little into LINQ statements.
bool CheckGameObjectPosition(Vector3 position)
{
//Check the zero position first
var zeroPosition = Vector3.zero;
if (position.x == zeroPosition.x && position.y == zeroPosition.y)
{
return true;
}
//Get all tile objects and select their position
var positions = GameObject.FindGameObjectsWithTag("Tiles")
.Select(f => f.transform.position);
//If there is any of the positions equal to our given position
if (positions.Any(f => f.x == position.x && f.y == position.y))
{
return true;
}
//Else we return false
return false;
}
The Problem is the float error for example it is 0.7 and it transforms to 0.6999999
Then there is nothing wrong with the function? You can additionally round of the positions
Vector3 roundedPos = new Vector3($$anonymous$$ath.Round(position.x, 1), $$anonymous$$ath.Round(position.y, 1), 0);
//the 1 in the math.round means 1 decimal place
Your answer
Follow this Question
Related Questions
How do i instantiate a Vector3[] ?? 2 Answers
Can't figure class property 0 Answers
Distribute terrain in zones 3 Answers
Vector3.MoveTowards doesnt work 1 Answer
Vector3.distance is always returning 0 4 Answers