如何让相机的垂直旋转使相机上下移动?
Posted
技术标签:
【中文标题】如何让相机的垂直旋转使相机上下移动?【英文标题】:How to let camera's vertical rotation move the camera up and down? 【发布时间】:2021-03-11 06:31:31 【问题描述】:我在游戏中有一个玩家,我可以用键盘移动它,用鼠标只能在水平轴上旋转。也就是说,我只能水平瞄准,不能上下瞄准。
我有 Cinemachine 的主摄像头和另一个 VM 摄像头。游戏目前的状态是这样的:
在水平轴上,我旋转播放器,但在垂直轴上,我只想上下移动播放器的相机/FOV。
我附加到播放器的移动脚本是:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
public CharacterController characterController;
public float speed = 35f;
public Animator animator;
// camera and rotation
public Transform cameraHolder;
public float mouseSensitivity = 2f;
public float upLimit = 50;
public float downLimit = -50;
// gravity
private float gravity = 9.87f;
private float verticalSpeed = 0;
void Update()
Move();
Rotate();
public void Rotate()
float horizontalRotation = Input.GetAxis("Mouse X");
float verticalRotation = Input.GetAxis("Mouse Y");
transform.Rotate(0, horizontalRotation * mouseSensitivity, 0);
cameraHolder.Rotate(-verticalRotation * mouseSensitivity, 0, 0);
Vector3 currentRotation = cameraHolder.localEulerAngles;
if (currentRotation.x > 180) currentRotation.x -= 360;
currentRotation.x = Mathf.Clamp(currentRotation.x, upLimit, downLimit);
cameraHolder.localRotation = Quaternion.Euler(currentRotation);
private void Move()
float horizontalMove = Input.GetAxis("Horizontal");
float verticalMove = Input.GetAxis("Vertical");
if (characterController.isGrounded) verticalSpeed = 0;
else verticalSpeed -= gravity * Time.deltaTime;
Vector3 gravityMove = new Vector3(0, verticalSpeed, 0);
Vector3 move = transform.forward * verticalMove + transform.right * horizontalMove;
characterController.Move(speed * Time.deltaTime * move + gravityMove * Time.deltaTime);
【问题讨论】:
【参考方案1】:这是我使用的代码,它适用于我,它非常易于实现,并且在您不专注于游戏时停止移动相机,这是 Brackey 的一个教程中为此目的而修改的脚本:
using UnityEngine;
public class CameraController : MonoBehaviour
public PlayerController player;
public float sensitivity = 150f;
public float clampAngle = 85f;
public bool look = true;
private float verticalRotation;
private float horizontalRotation;
private void Start()
verticalRotation = transform.localEulerAngles.x;
horizontalRotation = player.transform.eulerAngles.y;
// Defines the state of the cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
private void Update()
// Looks around if the user is in the window
if (look)
Look();
Debug.DrawRay(transform.position, transform.forward * 2, Color.red);
// If the player presses ESC while in the game, it unlocks the cursor
if (look && Input.GetKeyDown(KeyCode.Escape))
look = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
else if (Input.GetMouseButtonDown(0) && !look)
look = true;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
private void Look()
float _mouseVertical = -Input.GetAxis("Mouse Y");
float _mouseHorizontal = Input.GetAxis("Mouse X");
verticalRotation += _mouseVertical * sensitivity * Time.deltaTime;
horizontalRotation += _mouseHorizontal * sensitivity * Time.deltaTime;
verticalRotation = Mathf.Clamp(verticalRotation, -clampAngle, clampAngle);
transform.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
player.transform.rotation = Quaternion.Euler(0f, horizontalRotation, 0f);
【讨论】:
只有相机而不是播放器上下旋转是行不通的。以上是关于如何让相机的垂直旋转使相机上下移动?的主要内容,如果未能解决你的问题,请参考以下文章