Advertisement
johnnygoodguy2000

MovingPlatform.cs

May 16th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MovingPlatform : MonoBehaviour
  6. {
  7.  
  8.     public GameObject platform;// Reference to the platform object
  9.  
  10.     public float moveSpeed;// Speed at which the platform moves
  11.  
  12.     public Transform currentPoint;// Reference to the current point
  13.  
  14.     public Transform[] points;// Reference to the points array
  15.  
  16.     public int pointSelection;// Reference to the point selection
  17.  
  18.     // Start is called before the first frame update
  19.     void Start()
  20.     {
  21.         currentPoint = points[pointSelection];// Set the current point to the first point in the points array
  22.        
  23.     }
  24.  
  25.     // Update is called once per frame
  26.     void Update()
  27.     {
  28.         platform.transform.position = Vector3.MoveTowards(platform.transform.position, currentPoint.position,  Time.deltaTime * moveSpeed );// Move the platform towards the current point
  29.        
  30.         if (platform.transform.position == currentPoint.position)// If the platform has reached the current point
  31.         {
  32.             pointSelection++;// Increment the point selection
  33.  
  34.             if (pointSelection == points.Length)// If the point selection is equal to the length of the points array
  35.             {
  36.                 pointSelection = 0;// Set the point selection to 0
  37.             }
  38.  
  39.             currentPoint = points[pointSelection];// Set the current point to the next point in the points array
  40.         }
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement