Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Stew_79, Apr 29, 2021.

  1. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    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
    Code (CSharp):
    1.  if (direction == shipDirection.up) //******************************* Move Enemys UP
    2.         {
    3.             gameSpeed += gameController.theScrollSpeed * Time.deltaTime;
    4.  
    5.             setStopPosition();
    6.             stopPosition = stopY;
    7.             stopPosition = new Vector3(stopY.x + deployRate, stopY.y, stopY.z);
    8.             transform.position = Vector3.MoveTowards(transform.position, stopPosition, shipSpeed * Time.deltaTime);
    9.             CheckStopPosition();
    10.            
    11.         }
    12.  
    13.         if (direction == shipDirection.down) //******************************** Move Enemys DOWN
    14.         {
    15.            
    16.          
    17.             setStopPosition();
    18.             stopPosition = new Vector3(stopY.x  + deployRate, -stopY.y, stopY.z);
    19.             transform.position = Vector3.MoveTowards(transform.position,stopPosition, shipSpeed * Time.deltaTime);
    20.             CheckStopPosition();
    21.            
    22.         }
    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
    Code (CSharp):
    1. void CheckStopPosition()
    2.     {
    3.  
    4.         float dist = Vector3.Distance(firstStop, transform.position);
    5.        
    6.  
    7.        
    8.         if (transform.position == stopPosition)
    9.         {
    10.             Debug.Log("we are at stop " + transform.position);
    11.             Debug.Log("We should of stopped at " + stopPosition);
    12.             Debug.Log(dist);
    13.             direction = shipDirection.stop;
    14.            //transform.position = new Vector3(transform.position.x + gameController.theScrollSpeed * Time.deltaTime, transform.position.y);
    15.         }
    Code (CSharp):
    1. if (direction == shipDirection.stop) //************************************** STOP
    2.         {
    3.          
    4.             if (otherCheckOnce == false)
    5.             {
    6.                 Debug.Log("I have stopped at " + transform.position); // This is only so we get one output of debug.log and not 1 every frame
    7.                 otherCheckOnce = true;
    8.             }
    9.             //this holds the enemy position relitave to the camera
    10.             //
    11.             transform.position = new Vector3(transform.position.x + gameController.theScrollSpeed * Time.deltaTime, transform.position.y);
    12.         }
    I want the enemys to stop in a straight line like in the second video but hold that position whilst the camera is scrolling
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    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.
     
  3. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    Thank kurt but I have tried this
    Code (CSharp):
    1.  if (dist > 6f)
    But only the first two enemys hold their position and rest continue to move
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    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.
     
  5. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    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
     
  6. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    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