Advertisement
JonneOpettaja

MovingPlatform.cs

Feb 4th, 2019
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MovingPlatform : MonoBehaviour {
  5.  
  6.     public GameObject platform; //liikutettava objekti
  7.     public float moveSpeed; //liikkumisnopeus
  8.     public Transform currentPoint; //Senhetkinen lähtöpiste, ei tarvitse itse määrittää
  9.     public Transform[] points; // Array jolla määritetään pisteiden määrä
  10.     public int pointSelection; // Seuraava piste
  11.  
  12.     // Use this for initialization
  13.     void Start () {
  14.         currentPoint = points[pointSelection]; //mistä platform lähtee
  15.     }
  16.    
  17.     // Update is called once per frame
  18.     void Update () {
  19.         platform.transform.position = Vector3.MoveTowards(platform.transform.position, currentPoint.position,
  20.            Time.deltaTime * moveSpeed); //itse liikutus-skripti, liikuttaa nykyisestä pisteestä kohti kohdetta
  21.  
  22.         if(platform.transform.position == currentPoint.position) //jos kohdepiste saavutettu
  23.         {
  24.             pointSelection++; //siirrytään seuraavaan kohteeseen
  25.             if (pointSelection == points.Length) //jos viimeinen piste saavutettu
  26.             {
  27.                 pointSelection = 0; //kohteeksi tulee ensimmäinen piste
  28.             }
  29.             currentPoint = points[pointSelection];
  30.         }
  31.  
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement