Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MoveOnPath : MonoBehaviour
- {
- public EditPath PathToFollow;
- public int CurrentWayPointID = 0;
- public float speed;
- private float reachDistance = 1.0f;
- public float rotationSpeed = 5.0f;
- Vector3 last_Position;
- Vector3 current_Position;
- private void Start()
- {
- last_Position = transform.position;
- }
- private void Update()
- {
- float distance = Vector3.Distance(PathToFollow.path_objs[CurrentWayPointID].position, transform.position);
- transform.position = Vector3.MoveTowards(transform.position, PathToFollow.path_objs[CurrentWayPointID].position, Time.deltaTime * speed);
- var rotation = Quaternion.LookRotation(PathToFollow.path_objs[CurrentWayPointID].position - transform.position);
- transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
- if(distance <= reachDistance)
- {
- CurrentWayPointID++;
- }
- if(CurrentWayPointID >= PathToFollow.path_objs.Count)
- {
- transform.position = PathToFollow.path_objs[0].position;
- CurrentWayPointID = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement