fr0stn1k

Unity3(Task4)_Kovylov

Mar 8th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4.  
  5. public class BetweenPoints : MonoBehaviour
  6. {
  7.     public Vector3[] Points =
  8.     {
  9.         new Vector3(0.0f, 0.0f , 0.0f),
  10.         new Vector3(0.0f, 2.0f , 0.0f),
  11.         new Vector3(1.0f, 1.5f , 0.0f),
  12.     };
  13.     public float Speed = 1;
  14.     private int iterator;
  15.     private float Path;
  16.    
  17.     // Update is called once per frame
  18.     void Update ()
  19.     {
  20.         Path = Time.deltaTime * Speed;
  21.  
  22.         if (!isReachedPoint(iterator))
  23.         {
  24.             MoveToPoint(iterator);
  25.         }
  26.         if (isReachedPoint(iterator) && iterator != Points.Length - 1)
  27.         {
  28.             iterator++;
  29.         }
  30.         else if (isReachedPoint(Points.Length - 1))
  31.         {
  32.             iterator = 0;
  33.         }
  34.     }
  35.  
  36.     private bool isReachedPoint(int i)
  37.     {
  38.         return Math.Abs(transform.position.x - Points[i].x) <= 0.1f
  39.                && Math.Abs(transform.position.y - Points[i].y) <= 0.1f
  40.                && Math.Abs(transform.position.z - Points[i].z) <= 0.1f;
  41.  
  42.     }
  43.  
  44.     private void MoveToPoint(int i)
  45.     {
  46.         transform.Translate((Points[i] - transform.position).normalized * Path);
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment