Search Unity

Resolved HowTo: Object move left and right at spawn position

Discussion in 'Scripting' started by iimuli, May 4, 2022.

  1. iimuli

    iimuli

    Joined:
    Apr 21, 2022
    Posts:
    16
    Hey! I'm trying to make my target move left and right at the position that its randomly instantiated to with my Game Manager script. I'm currently using this for object movement:



    Code (CSharp):
    1. private float moveSpeed = 10;
    2.     private bool movingLeft;
    3.  
    4.     void Start()
    5.     {
    6.         movingLeft = true;
    7.     }
    8.     void Update()
    9.      {
    10.          if (movingLeft == true)
    11.          {
    12.              transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
    13.              if (transform.position.x <= - 4) movingLeft = false;
    14.          }
    15.          else
    16.          {
    17.              transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
    18.              if (transform.position.x >= + 4) movingLeft = true;
    19.          }
    20.      }


    The problem with this is that the spawned object goes to global 0X coord and moves between -4x and +4x there instead of moving at the position that it was randomly spawned to.

    I also tried to make public float posX; with posX = transform.position.x; variable at void start and doing "if (posX <= - 4) movingLeft = false;...." instead, but then they kept moving on -x Axis to infinity and beyond. What am I doing wrong? How to fix it? Beginner here, so please explain it clearly if possible :) Thank you in advance!

    EDIT: i dont know how to make the script look more readable here..
     
    Last edited: May 4, 2022
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Vector3.left is a hardcoded value of (-1, 0, 0). That's why your object is moving from the center of the coordinate system.
    Use something like the following:
    transform.Translate(transform.position.x + Vector3.left * moveSpeed * Time.deltaTime

    The code above simply adds your current position before moving the object.
     
  3. Deleted User

    Deleted User

    Guest

    There is an option to insert code into your post so it's more readable.

    upload_2022-5-4_10-53-0.png
     
  4. iimuli

    iimuli

    Joined:
    Apr 21, 2022
    Posts:
    16

    Hey! I tried this method and I got this error:
    Error CS0019 Operator '+' cannot be applied to operands of type 'float' and 'Vector3'

    I also tried to change:
    Code (CSharp):
    1. transform.Translate(transform.position.x + Vector3.left * moveSpeed * Time.deltaTime
    Into this:
    Code (CSharp):
    1. transform.position += Vector3.left * moveSpeed * Time.deltaTime;
    But the result remained the same


    Tried a few different ways trying to make it work and googling how to convert float to vector3 etc but im still struggling..

    And thank you Jnsptz for the tip!
     
    Last edited: May 4, 2022
  5. Deleted User

    Deleted User

    Guest

    Try this.



    Code (CSharp):
    1. transform.Translate((transform.position + Vector3.left) * (moveSpeed * Time.deltaTime));
     
  6. iimuli

    iimuli

    Joined:
    Apr 21, 2022
    Posts:
    16

    Hey! Tried the code you mentioned and it sent my objects flying to space at a super high speed the moment they were spawned :D

    I also tried this on the moveleft:
    Code (CSharp):
    1. transform.Translate(Time.deltaTime * -moveSpeed, 0, 0, Space.Self);
    but the results remained yet the same

    Could the problem be on these lines?
    Code (CSharp):
    1.  if (transform.position.x <= -4)
    2. if (transform.position.x >= 4)
     
    Last edited: May 4, 2022
  7. Deleted User

    Deleted User

    Guest

    Do you really want -4 and 4? That would suggest the object is spawned at 0, 0, 0 and you want it to move back and forth by four units.

    This might be better. Keep track of the initial spawn position and move the object based on a certain offset, ex. original X position +/- 4 units.

    This should move it at 1 unit per second. You can always use (Time.deltaTime * movementSpeed) if you want to speed it up.

    Code (CSharp):
    1. private float moveSpeed = 10;
    2.  
    3. private Vector3 initialSpawnPosition;
    4.  
    5. private bool movingLeft;
    6.  
    7. void Start()
    8. {
    9.     initialSpawnPosition = transform.position;
    10.     movingLeft = true;
    11. }
    12.  
    13. void Update()
    14. {
    15.     if (movingLeft)
    16.     {
    17.         transform.Translate((transform.position + Vector3.left) * Time.deltaTime);
    18.  
    19.         if (transform.position.x == initialSpawnPosition.x - 4.0f)
    20.         {
    21.             movingLeft = false;
    22.             movingRight = true;
    23.         }
    24.     }
    25.  
    26.     if (movingRight)
    27.     {
    28.         transform.Translate((transform.position + Vector3.right) * Time.deltaTime);
    29.  
    30.         if (transform.position.x == initialSpawnPosition.x + 4.0f)
    31.         {
    32.             movingRight = false;
    33.             movingLeft = true;
    34.         }
    35.     }
    36.  
    37. }
     
  8. iimuli

    iimuli

    Joined:
    Apr 21, 2022
    Posts:
    16
    Hey! Thank you a ton, your suggestion once again made my object fly upwards but with a little tweak to it i was finally able to made it work the way i intended. Here is my final code, making the spawned objects move left to right on x axis at the spot they were instantiated on!

    Code (CSharp):
    1.  
    2.     private Vector3 initialSpawnPosition;
    3.     private float moveSpeed = 10;
    4.     private bool movingLeft;
    5.  
    6.  
    7.     void Start()
    8.     {
    9.         initialSpawnPosition = transform.position;
    10.         movingLeft = true;
    11.  
    12.        
    13.     }
    14.     void Update()
    15.     {
    16.  
    17.          
    18.         if (movingLeft)
    19.         {
    20.             transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
    21.  
    22.             if(transform.position.x <= initialSpawnPosition.x - 4.0f)
    23.             {
    24.                movingLeft = false;
    25.             }
    26.         }
    27.         else
    28.         {
    29.             transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
    30.  
    31.             if (transform.position.x >= initialSpawnPosition.x + 4.0f)
    32.              {
    33.                movingLeft = true;
    34.              }
    35.         }
    36.          
    37.        
    38.    
     
    Deleted User likes this.
  9. Deleted User

    Deleted User

    Guest

    Just realised I made the mistake of adding the transform position when moving which would constantly increase the speed every frame.

    Code (csharp):
    1. transform.position + Vector3.left
    So if the position was 10, 10, 10 it would move it by 11 units per second. Then 12 units/second...etc. Multiplying that by movement speed no doubt made it worse because it was probably moving at 50+ units per second.

    Glad you got it to work.
     
    iimuli likes this.