unity控制摄像机几种视角实现方式
创始人
2025-01-09 09:05:48
0

目录

1、按下鼠标右键可以实现摄像机上下左右旋转

2、自由视角

3、摄像头跟随视角

4、跟随自由视角

5、第一人称跟随视角

python学习汇总连接:



1、按下鼠标右键可以实现摄像机上下左右旋转

这段代码定义了一个名为CameraRotate的脚本,用于控制摄像机根据鼠标右键(中键)的移动进行旋转。摄像机将以一定的旋转速度(rotationSpeed)跟随鼠标输入,并且其垂直旋转角度将被限制在最小垂直角度(minVerticalAngle)和最大垂直角度(maxVerticalAngle)之间,以防止过度倾斜。通过使用Quaternion.Lerp函数,摄像机的旋转过程更加平滑自然。

using System.Collections; using System.Collections.Generic; using UnityEngine;   public class CameraRotate : MonoBehaviour {     //旋转速度     public float rotationSpeed = 5f;     //上下旋转角度限制     public float maxVerticalAngle = 90f;     public float minVerticalAngle = -90f;     //旋转缓冲速度     public float lerpSpeed = 10f;     private float targetRotationX = 0f;     private float targetRotationY = 0f;     void Update()     {         if (Input.GetMouseButton(1))         {             // 获取鼠标输入的旋转增量             float rotationXInput = -Input.GetAxis("Mouse Y");             float rotationYInput = Input.GetAxis("Mouse X");             // 根据旋转速度进行摄像机的旋转             targetRotationX += rotationXInput * rotationSpeed;             targetRotationY += rotationYInput * rotationSpeed;             // 对上下旋转角度进行限制             targetRotationX = Mathf.Clamp(targetRotationX, minVerticalAngle, maxVerticalAngle);             // 根据旋转角度更新摄像机的欧拉角,Quaternion.Lerp可以使摄像机旋转更加平滑             Quaternion targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0f);             transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);         }     } }

2、自由视角

CameraRotate脚本同样实现摄像机围绕某个物体(target)旋转的功能,当按下鼠标右键时,摄像机会随着鼠标Y轴的移动而上下旋转、X轴的移动而左右旋转,同时确保旋转角度保持在设定范围内。此外,无论摄像机如何旋转,都会始终保持与目标物体之间的特定距离(distance),使摄像机始终围绕物体进行轨道式运动。

using System.Collections; using System.Collections.Generic; using UnityEngine;   public class CameraRotate : MonoBehaviour {     public Transform target;     public float rotationSpeed = 5f;     public float maxVerticalAngle = 90f;     public float minVerticalAngle = -90f;     public float lerpSpeed = 200f;     public float distance = 10;       private float targetRotationX = 0f;     private float targetRotationY = 0f;       void Start()     {         if (target == null)             Debug.LogError("Please assign a target to the orbit camera!");     }     void Update()     {         if (Input.GetMouseButton(1))         {             float rotationXInput = -Input.GetAxis("Mouse Y");             float rotationYInput = Input.GetAxis("Mouse X");               targetRotationX += rotationXInput * rotationSpeed;             targetRotationY += rotationYInput * rotationSpeed;               targetRotationX = Mathf.Clamp(targetRotationX, minVerticalAngle, maxVerticalAngle);               Quaternion targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0f);             transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);         }         transform.position = target.position - transform.forward * distance;     } }

3、摄像头跟随视角

CameraRotate脚本使得摄像机始终跟随在一个指定的目标物体后面,并保持在其正上方一定高度的位置(followHeight)。摄像机位置会以平滑的方式逐渐调整至理想状态,即目标物体正后方特定距离处,通过SmoothDamp函数实现了摄像机跟随过程的平滑过渡。最后,摄像机的方向始终面向目标物体,保证了稳定的跟随视角。

using System.Collections; using System.Collections.Generic; using UnityEngine;   public class CameraRotate : MonoBehaviour {     public Transform target;     public float followSpeed = 2f;     public float followHeight = 4f;     public float distance = 8f;       private Vector3 velocity = Vector3.zero;     void Start()     {         if (target == null)             Debug.LogError("Please assign a target to the orbit camera!");     }     void LateUpdate()     {         Vector3 targetPosition = target.position - (target.forward * distance)+new Vector3(0,followHeight,0);         transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, 1f / followSpeed);         transform.LookAt(target);     } }

4、跟随自由视角

在Unity中,实现一个跟随角色的摄像头,并且允许玩家自由旋转视角,通常分为两部分:

  1. 摄像机跟随角色移动
  2. 鼠标控制摄像机自由旋转

下面是一个简单的示例脚本,该脚本将实现这两个功能:

using UnityEngine;  public class CameraFollowAndRotate : MonoBehaviour {     public Transform target; // 要跟随的目标对象(例如:角色)     public float followDistance = 5f; // 跟随距离     public float heightOffset = 2f; // 高度偏移     public float rotationSpeed = 100f; // 视角旋转速度     public float xRotationLimit = 90f; // 水平旋转角度限制      private Vector3 offset;     private Quaternion originalRotation;      void Start()     {         // 初始化时记录下摄像机相对于目标初始的位置和旋转         offset = transform.position - target.position;         originalRotation = Quaternion.Euler(transform.eulerAngles.x, target.eulerAngles.y, 0);                  // 如果需要锁定鼠标光标         Cursor.lockState = CursorLockMode.Locked;     }      void LateUpdate()     {         // 跟随目标移动         Vector3 desiredPosition = target.position + offset;         desiredPosition.y += heightOffset;         transform.position = desiredPosition;          // 自由旋转视角         float mouseX = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;         float mouseY = Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;          // 限制X轴旋转         xRotation -= mouseY;         xRotation = Mathf.Clamp(xRotation, -xRotationLimit, xRotationLimit);          // 更新摄像机旋转         Quaternion yRotation = Quaternion.AngleAxis(mouseY, transform.right);         Quaternion xRotationQuat = Quaternion.AngleAxis(xRotation, Vector3.up);         transform.rotation = originalRotation * xRotationQuat * yRotation;     } }

在这个脚本中,摄像机会保持一定的距离和高度跟随目标对象。同时,通过监听鼠标的水平和垂直输入,允许玩家自由旋转摄像机视角。注意这个例子是第三人称跟随并自由旋转,如果是第一人称视角,处理方式会有所不同。

记得将此脚本挂载到摄像机上,并将target变量设置为你希望摄像机跟随的游戏对象

5、第一人称跟随视角

对于Unity中的第一人称跟随视角,摄像机通常与角色头部绑定,并通过鼠标控制视口的左右旋转。以下是一个简单的实现:

using UnityEngine;  public class FirstPersonCameraFollow : MonoBehaviour {     public Transform target; // 角色的头部或相机挂载点     public float mouseSensitivity = 100f;     public float yRotationLimit = 90f; // 可选:限制上下的旋转角度      private float xRotation = 0f;      void Start()     {         Cursor.lockState = CursorLockMode.Locked; // 锁定鼠标光标以获得更好的第一人称体验     }      void LateUpdate()     {         // 获取鼠标输入         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;          // 更新X轴(左右)旋转         xRotation -= mouseY;         xRotation = Mathf.Clamp(xRotation, -yRotationLimit, yRotationLimit);          // 更新Y轴(上下)旋转,直接应用到目标对象上         target.Rotate(Vector3.left * mouseY);          // 设置摄像机的局部旋转         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);          // 摄像机保持在角色头部位置         transform.position = target.position;     } }

在这个脚本中,摄像机将始终位于角色头部的位置,并且可以通过鼠标移动来改变视角的左右旋转。垂直旋转(抬头和低头)直接作用于角色头部,而不是摄像机自身,这是第一人称视角常见的处理方式。

注意,根据项目需求,可能需要调整摄像机的父级关系以及旋转限制等参数。同时,为了确保平滑的旋转效果,可以考虑使用Mathf.SmoothDampAngle函数进行插值处理。

python学习汇总连接:

50个开发必备的Python经典脚本(1-10)

50个开发必备的Python经典脚本(11-20)

50个开发必备的Python经典脚本(21-30)

50个开发必备的Python经典脚本(31-40)

50个开发必备的Python经典脚本(41-50)

相关内容

热门资讯

盘点一款!卡农大厅辅助,菜鸟黑... 盘点一款!卡农大厅辅助,菜鸟黑桃a3作弊,法门教程(有挂攻略)1、下载好卡农大厅辅助正确养号方法之后...
技术分享!广东闲来辅助神器免费... 技术分享!广东闲来辅助神器免费版,捉住捣蛋鸡作弊,举措教程(有挂详情)1、超多福利:超高返利,海量正...
必知教程!仟众部落辅助,冰球突... 必知教程!仟众部落辅助,冰球突破辅助软件,方案教程(果真有挂)1、完成仟众部落辅助辅助器v3.3的残...
最新研发!吉祥填大坑有插件吗,... 最新研发!吉祥填大坑有插件吗,四川途游破解安装包,大纲教程(有挂细节)1、四川途游破解安装包脚本辅助...
一分钟揭秘!吉祥填大坑辅助器攻... 一分钟揭秘!吉祥填大坑辅助器攻略,指尖四川辅助破解版,练习教程(有挂工具)1、吉祥填大坑辅助器攻略有...
总算了解!凑一桌小程序辅助器,... 总算了解!凑一桌小程序辅助器,开心游戏快跑辅助,办法教程(有挂助手)1、任何凑一桌小程序辅助器透视是...
一起来讨论!开心庄园脚本辅助器... 一起来讨论!开心庄园脚本辅助器,全来潜山跑风辅助器,妙计教程(有挂技巧)1、玩家可以在全来潜山跑风辅...
每日必备!朋友局app辅助器下... 每日必备!朋友局app辅助器下载,爱玩娱乐暗堡辅助,机巧教程(真的有挂)1)朋友局app辅助器下载辅...
终于清楚!蜀山四川小程序破解版... 终于清楚!蜀山四川小程序破解版下载,拱趴大菠萝技巧,步骤教程(竟然有挂)1、不需要AI权限,帮助你快...
大神推荐!潮友会辅助器,多乐辅... 大神推荐!潮友会辅助器,多乐辅助器免费版,手册教程(有挂实锤)1)潮友会辅助器有没有挂:进一步探索潮...