本文主要是介绍第一人称跟随视角与固定(2d)视角的转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
按下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);}}
}
这篇关于第一人称跟随视角与固定(2d)视角的转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!