Files
PrincessLandVR/Assets/FollowerController.cs
T
2021-01-12 22:55:14 -08:00

47 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class FollowerController : MonoBehaviour
{
public Transform target;
public float wakeDistance;
private NavMeshAgent agent;
private AudioSource audioSource;
private Animator[] anims;
private bool isFollowing = false;
void Start()
{
agent = GetComponent<NavMeshAgent>();
anims = GetComponentsInChildren<Animator>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (isFollowing)
{
foreach (var anim in anims)
{
anim.SetFloat("speed", agent.velocity.magnitude);
}
//Debug.Log($"Velocity: {agent.velocity.magnitude}");
agent.SetDestination(target.position);
}
else
{
float dist = Vector3.Distance(target.position, transform.position);
isFollowing = dist < wakeDistance;
if (isFollowing)
{
audioSource.Play();
Debug.LogWarning("Played the audio!");
}
}
}
}