Advertisement
Guest User

Move_Path

a guest
Apr 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement