Advertisement
lemansky

Untitled

Mar 1st, 2021
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CustomPatrol : MonoBehaviour
  6. {
  7.  
  8.     public float speed = 5.0f;
  9.     public Transform targetA = null, targetB = null; // Transform ще носи информация за позицията на двете точки, между които ще се движи платформата
  10.     bool changeDirection = false; // контролна променлива за смяна на посоката на движение
  11.  
  12.     void FixedUpdate()
  13.     {
  14.  
  15.         if (!changeDirection)
  16.         {
  17.             transform.position = Vector2.MoveTowards(transform.position, targetA.position, speed * Time.deltaTime);
  18.             // мястото на платформата ще се определя от мястото на игровия обект targetA
  19.         }
  20.  
  21.         if (changeDirection)
  22.         {
  23.             transform.position = Vector2.MoveTowards(transform.position, targetB.position, speed * Time.deltaTime);
  24.         }
  25.  
  26.         if (transform.position == targetA.position)
  27.         {
  28.             changeDirection = true;
  29.             // когато позицията на платформата съвпада с позицията на targetA ще се смени посоката чрез контролната променлива
  30.         }
  31.  
  32.         if (transform.position == targetB.position)
  33.         {
  34.             changeDirection = false;
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement