创建脚本并编写:PetFollowTarget.cs
using UnityEngine;
public class PetFollowTarget : MonoBehaviour{
Transform target;
float speed = 2f;
Animator animator;
void Start(){
target = GameObject.Find("PlayerNormal/PetsSmallPos").gameObject.transform;
animator = GetComponent<Animator>();
}
void Update(){
if (target != null){
Vector3 direction = target.position - transform.position;
float distance = direction.magnitude;
if (distance > 0.1f){
animator.SetBool("IsIdle", false);
transform.LookAt(target.position);
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
else{
animator.SetBool("IsIdle", true);
}
}
}
}
绑定脚本
宠物动画控制器
运行项目 - 主角移动宠物移动
End.