一、实现目标
实现上帝视角
二、实现方法
2.1:创建一个Camera
注意将Rotation.X设置为90 -- 俯视角
2.2创建一个C#脚本 -- GodViewCamera
编辑GodViewCamera
//version - 0.1
using UnityEngine;
public class GodViewCamera : MonoBehaviour
{
public float moveSpeed = 5f; // 摄像机移动速度
void Update()
{
Vector3 movement = Vector3.zero;
// WASD控制在XZ平面上的移动,忽略Y轴的上下移动
if (Input.GetKey(KeyCode.W))
movement.z += moveSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.S))
movement.z -= moveSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
movement.x -= moveSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.D))
movement.x += moveSpeed * Time.deltaTime;
// 保持Y轴高度不变,只在XZ平面上移动
//movement.y = 0;
// 应用移动
transform.Translate(movement, Space.World); // 使用Space.World确保在世界坐标系中移动
}
}
注:在version - 0.1中,我实现了X-Z轴上的移动,但是这种上帝视角不符合项目要求,需要更近一步,所以我在此基础上加上了旋转,并且将初始角度进行了调整。
//version - 0.2
using UnityEngine;
public class GodViewCamera : MonoBehaviour
{
public float moveSpeed = 5f; // 摄像机移动速度
public float mouseSensitivity = 4.0f; // 鼠标旋转灵敏度
private float xRotation = 45f;
void Update()
{
Vector3 movement = Vector3.zero;
// WASD控制在XZ平面上的移动,忽略Y轴的上下移动
if (Input.GetKey(KeyCode.W))
movement.z += moveSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.S))
movement.z -= moveSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
movement.x -= moveSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.D))
movement.x += moveSpeed * Time.deltaTime;
// 保持Y轴高度不变,只在XZ平面上移动
//movement.y = 0;
// 旋转:当鼠标右键被按下时,允许相机旋转
if (Input.GetMouseButton(1))
{
// 获取鼠标水平和垂直移动量,并应用旋转
//Input.GetAxis("Mouse X")是Unity引擎中用于获取鼠标水平方向移动的一个函数。
//这个函数返回一个浮点数,表示鼠标在X轴上的相对移动量。其取值范围通常是-1到1:
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
// 限制x轴旋转(俯仰)在-90到90度之间,防止翻转
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -45f, 45f);
// 直接使用transform来旋转相机,简化代码并移除对cameraTransform的依赖
transform.localRotation = Quaternion.Euler(xRotation, transform.localEulerAngles.y + mouseX, 0f);
// 注意:上面一行同时处理了x轴(俯仰)和y轴(偏航),简化了旋转逻辑
}
// 应用移动
transform.Translate(movement, Space.World); // 使用Space.World确保在世界坐标系中移动
}
}
注:在version - 0.2中,我发现一旦相机视角移动之后,WASD的移动仍是改变世界坐标系下X-Z值,所以我将代码近一步修改,确保在视角转动之后仍然可以正常移动。
//version - 0.3
using UnityEngine;
using UnityEngine.EventSystems;
public class GodViewCamera : MonoBehaviour
{
private Vector3 moveDirection = Vector3.zero;//初始化相机向量为0向量
public float moveSpeed = 5f; // 摄像机移动速度
public float mouseSensitivity = 4.0f; // 鼠标旋转灵敏度
private float xRotation = 45f;
void Update()
{
// 保存当前相机的y轴位置,确保之后的操作不影响y轴高度
float originalYPos = transform.position.y;
// 移动:结合水平和垂直输入计算相机移动方向
//horizontal 和 vertical 接收Input.GetAxis()返回的一个介于-1 到 1的数值
//表示方向和强度
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
// 注意这里我们直接使用transform,因为脚本假设挂载在相机上
//transform.right 返回的是相机对象本地坐标系的右侧方向向量。x- Axis
//transform.forward 返回的是相机对象本地坐标系的前方方向向量 y- Axis
moveDirection = transform.right * horizontal + transform.forward * vertical;
moveDirection *= moveSpeed * Time.deltaTime; // 根据时间增量调整移动距离以保持恒定速度
moveDirection.y = 0f;// 确保计算出的移动向量中y分量为0,避免影响高度
//print("moveDirection" + moveDirection);
transform.position += moveDirection; // 应用移动
//print("transform" + transform.position);
transform.position = new Vector3(transform.position.x, originalYPos, transform.position.z);
// 旋转:当鼠标右键被按下时,允许相机旋转
if (Input.GetMouseButton(1))
{
// 获取鼠标水平和垂直移动量,并应用旋转
//Input.GetAxis("Mouse X")是Unity引擎中用于获取鼠标水平方向移动的一个函数。
//这个函数返回一个浮点数,表示鼠标在X轴上的相对移动量。其取值范围通常是-1到1:
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
// 限制x轴旋转(俯仰)在-90到90度之间,防止翻转
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -45f, 45f);
// 直接使用transform来旋转相机,简化代码并移除对cameraTransform的依赖
transform.localRotation = Quaternion.Euler(xRotation, transform.localEulerAngles.y + mouseX, 0f);
// 注意:上面一行同时处理了x轴(俯仰)和y轴(偏航),简化了旋转逻辑
}
}
}