Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- public class BetweenPoints : MonoBehaviour
- {
- public Vector3[] Points =
- {
- new Vector3(0.0f, 0.0f , 0.0f),
- new Vector3(0.0f, 2.0f , 0.0f),
- new Vector3(1.0f, 1.5f , 0.0f),
- };
- public float Speed = 1;
- private int iterator;
- private float Path;
- // Update is called once per frame
- void Update ()
- {
- Path = Time.deltaTime * Speed;
- if (!isReachedPoint(iterator))
- {
- MoveToPoint(iterator);
- }
- if (isReachedPoint(iterator) && iterator != Points.Length - 1)
- {
- iterator++;
- }
- else if (isReachedPoint(Points.Length - 1))
- {
- iterator = 0;
- }
- }
- private bool isReachedPoint(int i)
- {
- return Math.Abs(transform.position.x - Points[i].x) <= 0.1f
- && Math.Abs(transform.position.y - Points[i].y) <= 0.1f
- && Math.Abs(transform.position.z - Points[i].z) <= 0.1f;
- }
- private void MoveToPoint(int i)
- {
- transform.Translate((Points[i] - transform.position).normalized * Path);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment