Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- [RequireComponent(typeof(NavMeshAgent))]
- public class PlayerMotor : MonoBehaviour
- {
- // Start is called before the first frame update
- Transform target;
- NavMeshAgent agent;
- void Start()
- {
- agent = GetComponent<NavMeshAgent>();
- }
- void Update()
- {
- if (target != null)
- {
- agent.SetDestination(target.position);
- FaceTarget();
- }
- }
- public void MoveToPoint(Vector3 movePoint)
- {
- agent.SetDestination(movePoint);
- }
- public void FollowTarget(Interactable newTarget)
- {
- agent.stoppingDistance = newTarget.radius * 0.75f;
- agent.updateRotation = false;
- target = newTarget.transform;
- }
- public void StopFollowTarget()
- {
- agent.stoppingDistance = 0f;
- agent.updateRotation = true;
- target = null;
- }
- void FaceTarget()
- {
- Vector3 direction = (target.position - transform.position).normalized;
- Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x,0f, direction.z));
- transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement