Advertisement
leomovskii

PlatformScript.cs

Oct 28th, 2021
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlatformScript : MonoBehaviour {
  6.  
  7.     public GameObject arrow;
  8.  
  9.     public bool isHorizontal = true;
  10.     public float speed = 2f;
  11.  
  12.     public float min;
  13.     public float max;
  14.  
  15.     bool directionPlus = true;
  16.  
  17.     void Start() {
  18.         SetDirection(true);
  19.     }
  20.        
  21.     void SetDirection(bool dir) {
  22.         directionPlus = dir;
  23.         float angle = isHorizontal ? (dir ? 0 : 180) : (dir ? 90 : 270);
  24.         arrow.transform.eulerAngles = new Vector3(arrow.transform.eulerAngles.x, arrow.transform.eulerAngles.y, angle);
  25.     }
  26.  
  27.     void FixedUpdate() {
  28.         if (isHorizontal) {
  29.             if (transform.position.x > max) {
  30.                 SetDirection(false);
  31.             } else if (transform.position.x < min) {
  32.                 SetDirection(true);
  33.             }
  34.         } else {
  35.             if (transform.position.y > max) {
  36.                 SetDirection(false);
  37.             } else if (transform.position.y < min) {
  38.                 SetDirection(true);
  39.             }
  40.         }
  41.  
  42.         float delta = speed * (directionPlus ? 1 : -1) * Time.deltaTime;
  43.            
  44.         if (isHorizontal) {
  45.             transform.position = new Vector3(transform.position.x + delta, transform.position.y, transform.position.z);
  46.         } else {
  47.             transform.position = new Vector3(transform.position.x, transform.position.y + delta, transform.position.z);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement