You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.1 KiB
C#

3 years ago
using UnityEngine;
namespace MalbersAnimations
{
[AddComponentMenu("Malbers/AI/Follow Target")]
/// <summary> Simple follow target for the animal </summary>
public class FollowTarget : MonoBehaviour
{
public Transform target;
public float stopDistance = 3;
ICharacterMove animal;
// Use this for initialization
void Start()
{
animal = GetComponentInParent<ICharacterMove>();
}
// Update is called once per frame
void Update()
{
Vector3 Direction = target.position - transform.position; //Calculate the direction from the animal to the target
float distance = Vector3.Distance(transform.position, target.position); //Calculate the distance..
animal.Move(distance > stopDistance ? Direction : Vector3.zero); //Move the Animal if we are not on the Stop Distance Radius
}
private void OnDisable()
{
animal.Move(Vector3.zero); //In case this script gets disabled stop the movement of the Animal
}
}
}