Unity3d入门代码--摄像机跟随

    xiaoxiao2021-04-15  31

    Unity3d入门代码–摄像机跟随

    摄像机跟随的代码


    代码

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class camerafollow : MonoBehaviour { public Transform target; //要跟随的目标 public float speed = 5f; //摄像机平滑速度 Vector3 offset; //目标与摄像机之间偏差值(距离) void Awake() { offset = transform.position - target.position; //计算出offset } void FixedUpdate() { Vector3 targetcampos = target.position + offset; //摄像机将要去的位置 transform.position = Vector3.Lerp(transform.position,targetcampos,speed*Time.deltaTime);//Lerp(现在的位置,目的地位置,持续时间) //Lerp函数是插值函数 } }

    最后不要忘记在unity编辑器中给target赋值


    转载请注明原文地址: https://ju.6miu.com/read-671383.html

    最新回复(0)