Advertisement
Cassimus

movment world

Feb 24th, 2025 (edited)
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class WorldMoveController : MonoBehaviour
  6. {
  7.     [SerializeField] private GameObject[] objectToMove;
  8.  
  9.     private GameObject currentStage;
  10.     private GameObject nextStage;
  11.  
  12.     private Vector2 startPosition;
  13.  
  14.     void Start()
  15.     {
  16.         startPosition = Vector3.zero;
  17.         currentStage = Instantiate(objectToMove[0], startPosition, Quaternion.identity, transform);
  18.         int nextStageIndex = Random.Range(0, objectToMove.Length);
  19.         nextStage =
  20.             Instantiate(objectToMove[nextStageIndex], new Vector2(startPosition.x + 16, startPosition.y),
  21.         Quaternion.identity, transform);
  22.  
  23.     }
  24.  
  25.     void Update()
  26.     {
  27.  
  28.         currentStage.transform.Translate(Vector3.left * GameManager.Instance.GetWorldSpeed() * Time.deltaTime);
  29.         nextStage.transform.Translate(Vector3.left * GameManager.Instance.GetWorldSpeed() * Time.deltaTime);
  30.  
  31.         if (currentStage.transform.position.x <= startPosition.x - 16)
  32.         {
  33.             var temp = currentStage;
  34.             currentStage = nextStage;
  35.             int nextStageIndex = Random.Range(0, objectToMove.Length);
  36.             nextStage = Instantiate(objectToMove[nextStageIndex], new Vector2(startPosition.x + 16, startPosition.y),
  37.                 Quaternion.identity, transform);
  38.             Destroy(temp);
  39.         }
  40.  
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement