unity实现第一视角的跟随物体运动

本文介绍了一个Unity中的摄像机跟随脚本,通过调整距离和高度参数,可以改变摄像机相对于目标物体的位置,实现平滑的跟随效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System.Collections;using System.Collections.Generic;using UnityEngine;public class FirstCamera : MonoBehaviour { public Transform target; public float distance; public float targetHeight; public float PitchAngle; private float x = 0.0f; private float y = 0.0f; // Use this for initialization void Start () { var angles = transform.eulerAngles; x = angles.x; y = angles.y; } // Update is called once per frame void LateUpdate() { if (!target) return; y = target.eulerAngles.y; // ROTATE CAMERA: Quaternion rotation = Quaternion.Euler(x - PitchAngle, y, 0); transform.rotation = rotation; // POSITION CAMERA: Vector3 position = target.position - (rotation * Vector3.forward * distance + new Vector3(0, -targetHeight, 0)); transform.position = position; }}创建一个摄像机,使其成为物体的子物体,将此脚本拖到摄像机下,可以在摄像机的Inspector面板如下图,修改distince和Target Height调整摄像机角度位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值