https://blog.csdn.net/u011484013/article/details/53931181
玩家:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
if(h!=0 || v!=0){
transform.LookAt (transform.position+new Vector3(h,0,v));
transform.Translate (transform.forward*Time.deltaTime*5f,Space.World);
}
}
}
宠物“:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PetMove : MonoBehaviour {
public Transform player;//主角
public float speed=1f;//移动的阻尼,值越小,移动越平缓
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {//如果主角和宠物直接的距离大于5,控制宠物跟随主角移动
if(Vector3.Distance(player.position,transform.position)>5f){
PetSmothFlow ();
//to do。。播放移动动画
}
//to do。。播放站立动画
//控制宠物的朝向
transform.LookAt (player.position);
}
//控制宠物的位置平滑移动
void PetSmothFlow(){
transform.position=Vector3.Lerp (transform.position,player.position,Time.deltaTime*speed);
}
}
来源:CSDN
作者:卍涣
链接:https://blog.csdn.net/qq_42453562/article/details/103808673