Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(NavMeshAgent))]
- [RequireComponent(typeof(ThirdPersonCharacter))]
- public class AICharacterControl : MonoBehaviour {
- public NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding
- public ThirdPersonCharacter character { get; private set; } // the character we are controlling
- public Transform target; // target to aim for
- public float targetChangeTolerance = 1f; // distance to target before target can be changed
- Vector3 targetPos;
- // Use this for initialization
- void Start () {
- // get the components on the object we need ( should not be null due to require component so no need to check )
- agent = GetComponentInChildren<NavMeshAgent>();
- character = GetComponent<ThirdPersonCharacter>();
- }
- // Update is called once per frame
- void Update () {
- if (target != null)
- {
- // update the progress if the character has made it to the previous target
- if ((target.position-targetPos).magnitude > targetChangeTolerance) {
- targetPos = target.position;
- agent.SetDestination(targetPos);
- }
- // update the agents posiiton
- agent.transform.position = transform.position;
- // use the values to move the character
- character.Move( agent.desiredVelocity, false, false, targetPos );
- } else {
- // We still need to call the character's move function, but we send zeroed input as the move param.
- character.Move ( Vector3.zero, false, false, transform.position + transform.forward * 100 );
- }
- }
- public void SetTarget(Transform target)
- {
- this.target = target;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement