Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。
镜头的旋转设置
首先为镜头设置一个父类,通过父类的Y值设置镜头左右的旋转,在通过镜头本身的X值控制镜头上下的旋转。
代码:
if (Input.GetMouseButton(1))
{
float mouseX = Input.GetAxis("Mouse X") * sleeps;
float mouseY = Input.GetAxis("Mouse Y") * sleeps;
Quaternion rotationX = transform.localRotation * Quaternion.Euler(0, mouseX, 0);
Quaternion rotationY = HeadObj.transform.localRotation * Quaternion.Euler(-mouseY, 0, 0);
transform.localRotation = Quaternion.Lerp(transform.localRotation, rotationX, 1);
HeadObj.transform.localRotation = Quaternion.Lerp(HeadObj.transform.localRotation, rotationY, 1);
}
镜头的移动:
镜头移动我们主要通过transform.Translate来设置,当点击W的时候,镜头朝向摄像机正前方移动。点击S的时候朝向镜头的后方移动,而不是单纯的水平移动。AD为左右水平的移动,EQ为上下的垂直移动。
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
float x = moveH * Time.deltaTime * sleeps;
float z = moveV * Time.deltaTime * sleeps;
Vector3 v1 = transform.GetChild(0).forward;
transform.Translate(v1 * z , Space.World);
Vector3 v2 = transform.right;
transform.Translate(v2 * x, Space.World);
当然我们肯定不想镜头在场景中无限制的移动。要给镜头一个上下左右前后的一个移动范围。接下来上完整的代码。
完整代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuanJingCamera : MonoBehaviour
{
public float sleeps;
public Transform HeadObj;
[Header("限制镜头左右的范围")]
public float Min_x;
public float Max_x;
public float Min_z;
public float Max_z;
[Header("限制镜头上下的范围")]
public float Min_y;
public float Max_y;
void Start()
{
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
float x = moveH * Time.deltaTime * sleeps;
float z = moveV * Time.deltaTime * sleeps;
//x = Mathf.Clamp(x, Min_x, Max_x);
//z = Mathf.Clamp(z, Min_z, Max_z);
if (transform.position.x <= Max_x
&& transform.position.x >= Min_x
&& transform.position.z <= Max_z
&& transform.position.z >= Min_z
)
{
Vector3 v1 = transform.GetChild(0).forward;
transform.Translate(v1 * z , Space.World);
Vector3 v2 = transform.right;
transform.Translate(v2 * x, Space.World);
//transform.Translate(Vector3.left * -z);
// transform.Translate(x, 0, z);
}
if (transform.position.x >= Max_x)
{
transform.position = new Vector3(Max_x, transform.position.y, transform.position.z);
}
if (transform.position.x <= Min_x)
{
transform.position = new Vector3(Min_x, transform.position.y, transform.position.z);
}
if (transform.position.z <= Min_z)
{
transform.position = new Vector3(transform.position.x, transform.position.y, Min_z);
}
if (transform.position.z >= Max_z)
{
transform.position = new Vector3(transform.position.x, transform.position.y, Max_z);
}
if (transform.position.y < Max_y)
// transform.position += new Vector3(0, Time.deltaTime * sleeps, 0);
{
if (Input.GetKey(KeyCode.E))
transform.Translate(0, Time.deltaTime * sleeps, 0, Space.World);
}
else
transform.position = new Vector3(transform.position.x, Max_y, transform.position.z);
if (transform.position.y > Min_y)
{
if (Input.GetKey(KeyCode.Q))
transform.Translate(0, Time.deltaTime * -sleeps, 0);
}
// transform.position += new Vector3(0, Time.deltaTime * -sleeps, 0);
else
transform.position = new Vector3(transform.position.x, Min_y, transform.position.z);
// transform.Translate(transform.position * moveH);
if (Input.GetMouseButton(1))
{
float mouseX = Input.GetAxis("Mouse X") * sleeps;
float mouseY = Input.GetAxis("Mouse Y") * sleeps;
Quaternion rotationX = transform.localRotation * Quaternion.Euler(0, mouseX, 0);
Quaternion rotationY = HeadObj.transform.localRotation * Quaternion.Euler(-mouseY, 0, 0);
transform.localRotation = Quaternion.Lerp(transform.localRotation, rotationX, 1);
HeadObj.transform.localRotation = Quaternion.Lerp(HeadObj.transform.localRotation, rotationY, 1);
}
}
}