您的位置:首页 > 移动开发 > Unity3D

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

2017-04-14 09:17 537 查看

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赋值

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity3d