Files
PrincessLandVR/Assets/FollowerController.cs
gunnbr 12145753f0 Make Shello rotate towards the player before jumping (this still needs work)
Convert all scripts to used tagged Player.
Try to keep Shello on the ground while walking (not sure if this works yet).
Fix transition from excitement to walking to happen immediately.
2021-01-17 10:22:11 -08:00

53 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class FollowerController : MonoBehaviour
{
public float wakeDistance;
private NavMeshAgent agent;
private AudioSource audioSource;
private Animator[] anims;
private bool isFollowing = false;
private Transform target;
void Start()
{
agent = GetComponent<NavMeshAgent>();
anims = GetComponentsInChildren<Animator>();
audioSource = GetComponent<AudioSource>();
var players = GameObject.FindGameObjectsWithTag("Player");
if (players?.Length > 0)
{
target = players[0].transform;
}
}
// 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!");
}
}
}
}