unity--跟随

纵饮孤独 提交于 2020-01-02 21:43:24

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);
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!