按下z键
把相机从玩家跟随的第一人称视角,换成固定在桌子上的固定视角
按下z键
在把视角换回来
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform player;
private float mouseX, mouseY; // 获取鼠标移动的值
public float mouseSensitivity; // 鼠标灵敏度
public float xRotation;
private Vector3 originalPosition;
private Quaternion originalRotation;
private bool isCameraFixed = false;
private void Start()
{
originalPosition = transform.localPosition;
originalRotation = transform.localRotation;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
{
isCameraFixed = !isCameraFixed;
if (isCameraFixed)
{
// 将相机变为非玩家的子物体
transform.SetParent(null);
// 移动并旋转到固定位置
transform.position = new Vector3(-18.7314f, 2.9f, 2.552323f);
transform.rotation = Quaternion.Euler(84.695f, -49.159f, 0f);
}
else
{
// 相机回到原来的位置跟随人物进行第一人称视角的移动和旋转
transform.SetParent(player);
transform.localPosition = originalPosition;
transform.localRotation = originalRotation;
}
}
if (!isCameraFixed)
{
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY; // 使上下移动时不出现卡顿
xRotation = Mathf.Clamp(xRotation, -70f, 70f); // 限制值在-70f至70之间,使玩家不会无限制上下转动视角
player.Rotate(Vector3.up * mouseX);
transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
}
}
}