找回密码
 立即注册
查看: 136|回复: 0

[简易教程] 自学Unity相机跟随人物代码含注释

[复制链接]
发表于 2024-7-15 18:43 | 显示全部楼层 |阅读模式
public class FollowTarget : MonoBehaviour
{
//角色位置
private Transform player;
//相机偏移量
private Vector3 offset;
//平滑度
private float smoothing = 3f;
private void Start()
{
//查找角色的位置=他的标签是Player.位置
player = GameObject.FindGameObjectWithTag(”Player”).transform;
//相机和人物的偏移量=相机位置-人物位置
offset = transform.position - player.position;
}
private void LateUpdate()
{
//相机跟随度 局部坐标转换世界坐标
Vector3 targetPosition = player.position + player.TransformDirection( offset);
//相机位置=向量插值(相机位置,相机跟随度,平滑度时间)
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothing);
//相机一直望着主角
transform.LookAt(player.position);
}
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-8 11:08 , Processed in 0.108973 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表