How to check if a game object is at a specific location

Hi guys
I am have trouble checking if a gameobeject is at a specific location to make this easier to explain I have made a couple of videos

This video shows what happens when I check the stopPosition

This video shows what happens when I don’t check the stopPosition

This The code I use to move the enemys to their stopPositions

 if (direction == shipDirection.up) //******************************* Move Enemys UP
        {
            gameSpeed += gameController.theScrollSpeed * Time.deltaTime;

            setStopPosition();
            stopPosition = stopY;
            stopPosition = new Vector3(stopY.x + deployRate, stopY.y, stopY.z);
            transform.position = Vector3.MoveTowards(transform.position, stopPosition, shipSpeed * Time.deltaTime);
            CheckStopPosition();
           
        }

        if (direction == shipDirection.down) //******************************** Move Enemys DOWN
        {
           
         
            setStopPosition();
            stopPosition = new Vector3(stopY.x  + deployRate, -stopY.y, stopY.z);
            transform.position = Vector3.MoveTowards(transform.position,stopPosition, shipSpeed * Time.deltaTime);
            CheckStopPosition();
           
        }

This is code I use to check if the enemys are at their stopPositions so that I can get them to hold their positions whilst the camera is scrolling

void CheckStopPosition()
    {

        float dist = Vector3.Distance(firstStop, transform.position);
       

       
        if (transform.position == stopPosition)
        {
            Debug.Log("we are at stop " + transform.position);
            Debug.Log("We should of stopped at " + stopPosition);
            Debug.Log(dist);
            direction = shipDirection.stop;
           //transform.position = new Vector3(transform.position.x + gameController.theScrollSpeed * Time.deltaTime, transform.position.y);
        }
if (direction == shipDirection.stop) //************************************** STOP
        {
         
            if (otherCheckOnce == false)
            {
                Debug.Log("I have stopped at " + transform.position); // This is only so we get one output of debug.log and not 1 every frame
                otherCheckOnce = true;
            }
            //this holds the enemy position relitave to the camera 
            //
            transform.position = new Vector3(transform.position.x + gameController.theScrollSpeed * Time.deltaTime, transform.position.y);
        }

I want the enemys to stop in a straight line like in the second video but hold that position whilst the camera is scrolling

Generally comparing Vector3 (positions) for equality is not going to work well due to floating point inaccuracies.

Instead, use Vector3.Distance() to see how close something is and decide it is close enough.

That also lets you display the distance in realtime to see if it is reaching the value you need it to.

Thank kurt but I have tried this

 if (dist > 6f)

But only the first two enemys hold their position and rest continue to move

Make sure to consider if perhaps they are stopping, then restarting.

Or if they are simply not stopping.

Or if something else is restarting them immediately.

Etc.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

If you are running a mobile device you can also see the console output. Google for how.

checking the distance in debug log I can see it is decreasing each time an enemy is reaching its stopPosition.
With only 8 enemeys spawned there distance from start position to there stopPosition ranges from 6.18107
to 5.810337
so I am not sure how to use Vector3 distance for this issue especially when spawning 32 or 64 enemys

I’m starting to think that I should of just made the background move instead of the camera. This would ilvaite this problem and a few others that I have encountered, but now that seems like so much work to fix