首页 > 代码库 > Unity3D 学习教程 14 C# 旋转镜头
Unity3D 学习教程 14 C# 旋转镜头
if(Input.GetKey(KeyCode.Q))
{
transform.Rotate(0,-50*Time.deltaTime,0,Space.Self);
}
if(Input.GetKey(KeyCode.E))
{
transform.Rotate(0,50*Time.deltaTime,0,Space.Self);
}
模仿WOW镜头操作
- using UnityEngine;
- using System.Collections;
- ///
- /// 将此脚本附加到任意镜头上,可以使其拥有WOW镜头的控制方式
- ///
- public class WowCamera : MonoBehaviour
- {
- ///
- /// 镜头目标
- ///
- public Transform Target;
- ///
- /// 镜头离目标的距离
- ///
- public float Distance = 30.0f;
- ///
- /// 最大镜头距离
- ///
- public float MaxDistance = 30.0f;
- ///
- /// 鼠标滚轮拉近拉远速度系数
- ///
- public float ScrollFactor = 10.0f;
- ///
- /// 镜头旋转速度比率
- ///
- public float RotateFactor = 10.0f;
- ///
- /// 镜头水平环绕角度
- ///
- public float HorizontalAngle = 45;
- ///
- /// 镜头竖直环绕角度
- ///
- public float VerticalAngle = 0;
- private Transform mCameraTransform;
- void Start()
- {
- mCameraTransform = transform;
- }
- void Update()
- {
- //滚轮向前:拉近距离;滚轮向后:拉远距离
- var scrollAmount = Input.GetAxis(GameSetting.MouseScrollWheel);
- Distance -= scrollAmount * ScrollFactor;
- //保证镜头距离合法
- if (Distance < 0)
- Distance = 0;
- else if (Distance > MaxDistance)
- Distance = MaxDistance;
- //按住鼠标左右键移动,镜头随之旋转
- var isMouseLeftButtonDown = Input.GetMouseButton(0);
- var isMouseRightButtonDown = Input.GetMouseButton(1);
- if (isMouseLeftButtonDown || isMouseRightButtonDown)
- {
- Screen.lockCursor = true;
- var axisX = Input.GetAxis(GameSetting.MouseX);
- var axisY = Input.GetAxis(GameSetting.MouseY);
- HorizontalAngle += axisX * RotateFactor;
- VerticalAngle += axisY * RotateFactor;
- if (isMouseRightButtonDown)
- {
- //如果是鼠标右键移动,则旋转人物在水平面上与镜头方向一致
- Target.rotation = Quaternion.Euler(0, HorizontalAngle, 0);
- }
- }
- else
- {
- Screen.lockCursor = false;
- }
- //按镜头距离调整位置和方向
- var rotation = Quaternion.Euler(-VerticalAngle, HorizontalAngle, 0);
- var offset = rotation * Vector3.back * Distance;
- mCameraTransform.position = Target.position + offset;
- mCameraTransform.rotation = rotation;
- }
- }
Unity3D 学习教程 14 C# 旋转镜头
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。