I would appreciate any help that can be provided as the reason this code isn’t working doesn’t make sense to me.
I have a collection/array of obstacles that are moving across the screen (x position) from right to left. I have a player/ship that is located on the left side of the screen.
I would like to kick off a method once one of the obstacles have passed the ship. When I run my code the function kicks off before the object even makes it to the middle of the screen. I don’t understand why this is happening.
My code is as follows:
void Update ()
{
CheckObstaclePosition();
}
void CheckObstaclePosition()
{
Obstacles[] obstaclesIns;
Obstacles currObstacle;
obstaclesIns = FindObjectsOfType();
if (obstaclesIns.Length > 0)
{
currObstacle = obstaclesIns[0];
currObstacleVector = currObstacle.transform.position;
currShipVector = this.gameObject.transform.position;
currObstaclePos = currObstacleVector.x;
currShipPos = currShipVector.x - 0.2f;
if (currObstaclePos < currShipPos)
{
Debug.Log("Obstacle passed Ship");
AnimateExplosion();
currObstacle.DestroyObstacle();
}
}
Can someone explain why this behavior is happening and why the x position of the obstacle is showing that it is passing the ship before it even gets close to the middle of the screen let alone actually getting pass the ship.