Advertisement
Psykikk

PlayerMotor Script

Dec 2nd, 2021
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. [RequireComponent(typeof(NavMeshAgent))]
  7. public class PlayerMotor : MonoBehaviour
  8. {
  9. // Start is called before the first frame update
  10. Transform target;
  11.  
  12. NavMeshAgent agent;
  13. void Start()
  14. {
  15. agent = GetComponent<NavMeshAgent>();
  16. }
  17. void Update()
  18. {
  19. if (target != null)
  20. {
  21. agent.SetDestination(target.position);
  22. FaceTarget();
  23. }
  24. }
  25.  
  26. public void MoveToPoint(Vector3 movePoint)
  27.  
  28. {
  29. agent.SetDestination(movePoint);
  30. }
  31. public void FollowTarget(Interactable newTarget)
  32. {
  33. agent.stoppingDistance = newTarget.radius * 0.75f;
  34. agent.updateRotation = false;
  35. target = newTarget.transform;
  36. }
  37. public void StopFollowTarget()
  38. {
  39. agent.stoppingDistance = 0f;
  40. agent.updateRotation = true;
  41. target = null;
  42. }
  43. void FaceTarget()
  44. {
  45. Vector3 direction = (target.position - transform.position).normalized;
  46. Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x,0f, direction.z));
  47. transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
  48. }
  49.  
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement