I need to code a script that has my target object change animations when the player comes within a certain distance. However, the object isn’t changing to the specified animation. Here’s my script. The ‘Target’ variable is the object I’m trying to get to change its animation. Thanks for any and all help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimateWhenPlayerIsNear : MonoBehaviour
{
private Animation anim;
public float dist;
public GameObject Target;
private Transform player;
private void Start()
{
player = GameObject.FindWithTag("Player").GetComponent<Transform>();
}
void Update()
{
dist = Vector3.Distance(player.position, Target.transform.position);
if (dist < 5.0f)
{
anim.Play("New Move");
}
}
}