SHARE
TWEET

Move_Path

a guest Apr 25th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MoveOnPath : MonoBehaviour
  6. {
  7.     public EditPath PathToFollow;
  8.  
  9.     public int CurrentWayPointID = 0;
  10.     public float speed;
  11.     private float reachDistance = 1.0f;
  12.     public float rotationSpeed = 5.0f;
  13.  
  14.     Vector3 last_Position;
  15.     Vector3 current_Position;
  16.  
  17.     private void Start()
  18.     {
  19.         last_Position = transform.position;
  20.     }
  21.  
  22.     private void Update()
  23.     {
  24.         float distance = Vector3.Distance(PathToFollow.path_objs[CurrentWayPointID].position, transform.position);
  25.         transform.position = Vector3.MoveTowards(transform.position, PathToFollow.path_objs[CurrentWayPointID].position, Time.deltaTime * speed);
  26.  
  27.         var rotation = Quaternion.LookRotation(PathToFollow.path_objs[CurrentWayPointID].position - transform.position);
  28.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
  29.  
  30.         if(distance <= reachDistance)
  31.         {
  32.             CurrentWayPointID++;
  33.         }
  34.  
  35.         if(CurrentWayPointID >= PathToFollow.path_objs.Count)
  36.         {
  37.             transform.position = PathToFollow.path_objs[0].position;
  38.             CurrentWayPointID = 0;
  39.         }
  40.     }
  41. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top