Advertisement
GoodNoodle

Mover

Jun 18th, 2021
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. public class Mover : MonoBehaviour, IAction
  7. {
  8.     Transform target;
  9.  
  10.     NavMeshAgent nav;
  11.  
  12.     private void Start()
  13.     {
  14.         nav = GetComponent<NavMeshAgent>();
  15.     }
  16.  
  17.  
  18.     void Update()
  19.     {
  20.         UpdateAnimator();
  21.     }
  22.  
  23.     public void StartMoveAction(Vector3 des)
  24.     {
  25.         GetComponent<ActionScedular>().StartAction(this);
  26.         MoveTo(des);
  27.     }
  28.  
  29.     public void MoveTo(Vector3 des)
  30.     {
  31.         GetComponent<NavMeshAgent>().destination = des;
  32.         nav.isStopped = false;
  33.     }
  34.  
  35.     public void Cancel()
  36.     {
  37.         nav.isStopped = true;
  38.     }
  39.     private void UpdateAnimator()
  40.     {
  41.         Vector3 velocity = GetComponent<NavMeshAgent>().velocity;
  42.         Vector3 localVel = transform.InverseTransformDirection(velocity);
  43.         float speed = localVel.z;
  44.         GetComponent<Animator>().SetFloat("ForwardSpeed", speed);
  45.     }
  46.  
  47.    
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement