Search Unity

Making a moving object change direction when it reaches a certain position?

Discussion in 'Scripting' started by jjamerson018, Jul 25, 2021.

  1. jjamerson018

    jjamerson018

    Joined:
    Jul 22, 2021
    Posts:
    6
    I've never written on a forum before so let me know if I'm posting to the wrong forum or doing something wrong.
    I'm trying to make a game where the clouds move from side to side in the background, and I want the clouds to start moving the opposite direction when they reach a certain position off-camera.

    Please let me know what improvements I can make, I'm new to all this haha.

    Edit: I uploaded the code properly this time, thanks for the advice!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CloudMovement_Right : MonoBehaviour
    6. {
    7. private Rigidbody rb;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.     public float speed = 2.0f;
    16.     private Vector3 endPosition = new Vector3(90, -2, 77);
    17.     private Vector3 startPosition = new Vector3(-90, -2, 77);
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if(transform.position == startPosition)
    23.         {
    24.             transform.Translate(Vector3.forward * Time.deltaTime * speed);
    25.         }
    26.         else if(transform.position == endPosition)
    27.         {
    28.             transform.Translate(Vector3.back * Time.deltaTime * speed);
    29.         }
    30.         else
    31.         {
    32.             transform.Translate(Vector3.forward * Time.deltaTime * speed);
    33.         }
    34.        
    35.     }
    36. }
    37.  
     
    Last edited: Jul 26, 2021
  2. jjamerson018

    jjamerson018

    Joined:
    Jul 22, 2021
    Posts:
    6
    I should probably mention, the y and z coordinates are correct to the specific cloud I am trying to apply this to, and the only changing coordinate will be my x-coordinate as it moves from side-to-side.
     
  3. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Please edit your post to make the code more readable. In the edit bar there is a word "Code:". Click the first icon to the right of it and post your code there rather than post it as it is. While i'm not going to be able to help you, others might be more inclined to help if the code is formatted properly. :)
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    As stated above, please use code-tags when posting code. As I'm sure you can see, unformatted code is difficult to follow.
     
  5. jjamerson018

    jjamerson018

    Joined:
    Jul 22, 2021
    Posts:
    6
    Thank you for letting me know! I updated it.
     
  6. jjamerson018

    jjamerson018

    Joined:
    Jul 22, 2021
    Posts:
    6
    Yeah I can see there's a pretty significant difference haha, it's updated now.