Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Zombie-Shooter/AICharacterControl.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
43 lines (34 sloc)
1.45 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
namespace UnityStandardAssets.Characters.ThirdPerson | |
{ | |
[RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))] | |
[RequireComponent(typeof (ThirdPersonCharacter))] | |
public class AICharacterControl : MonoBehaviour | |
{ | |
public UnityEngine.AI.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 | |
private 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<UnityEngine.AI.NavMeshAgent>(); | |
character = GetComponent<ThirdPersonCharacter>(); | |
agent.updateRotation = false; | |
agent.updatePosition = true; | |
} | |
private void Update() | |
{ | |
if (target != null) | |
agent.SetDestination(target.position); | |
if (agent.remainingDistance > agent.stoppingDistance) | |
character.Move(agent.desiredVelocity, false, false); | |
else | |
character.Move(Vector3.zero, false, false); | |
} | |
public void SetTarget(Transform target) | |
{ | |
this.target = target; | |
} | |
} | |
} |