这几天回顾整理的看了第三人称的游戏控制和摄像机控制的代码。
通过自己写的代码和对比别人写的代码,收获到的东西:
自己:功能随意的叠加,想到什么编辑什么,再查询时就感觉有点混乱。且喜欢建很多个CS些。通过如今再整合和观看了别的写法时…
别人:1)每个参数都给一个命名(能够做到查询有条理,也半边理解。(个人觉得也需要很强的英语水平,我都是只能靠猜靠翻译的理解))
2)尽可能的把主要的处理简单化,细化的步骤也统一的用函数封装起来。如图例子:
函数的封装使用起来也很方便。这就是我所要学习的编辑手法,还有熟悉那些痛苦的英语单词。
以下分享我窃取的大佬完美的封装代码(加了一些注释,都是百度翻译复制粘贴下来的。经供参考)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraCtr : MonoBehaviour
{
public Transform player;
public Vector3 pivotOffset = new Vector3(0.0f, 1.7f, 0.0f); // 偏移以重新定位摄像机。
public Vector3 camOffset = new Vector3(0.0f, 0.0f, -3.0f); // 偏移以重新定位与播放机位置相关的摄像机。
public float smooth = 10f; // 相机的反应速度。
public float maxVerticalAngle = 30f; // 摄像机最大夹角。
public float minVerticalAngle = -60f; // 摄像机最小夹角。
private Transform cam;
private float angleH = 0; // 浮动存储与鼠标移动相关的摄像机水平角度。
private float angleV = 0; // 浮动存储与鼠标移动相关的摄像机垂直角度。
private bool isCustomOffset; // 布尔值,以确定是否使用自定义相机偏移量。
private Vector3 smoothPivotOffset; // 相机电流枢轴偏移对插补。
private Vector3 smoothCamOffset; // 内插相机电流偏移量。
private Vector3 targetPivotOffset; // 摄像机支点偏移目标到迭代。
private Vector3 targetCamOffset; // 摄像机偏移目标进行插值。
private float targetMaxVerticalAngle; // 自定义摄像机最大垂直夹紧角
private float defaultFOV; // 默认摄像机视场。
private float targetFOV; // 目标摄像机视场。
// 获取相机水平角度。
public float GetH { get { return angleH; } }
// Start is called before the first frame update
void Awake()
{
// 对照相机变换的引用。
cam = transform;
// 设置相机默认位置。
cam.position = player.position + Quaternion.identity * pivotOffset + Quaternion.identity * camOffset;
cam.rotation = Quaternion.identity;
// 设置引用和默认值。
smoothPivotOffset = pivotOffset;
smoothCamOffset = camOffset;
defaultFOV = cam.GetComponent<Camera>().fieldOfView;
angleH = player.eulerAngles.y;
ResetTargetOffsets(); // 将相机偏移量重置为默认值。
ResetFOV(); // 将“视野”重置为默认值
ResetMaxVerticalAngle(); // 将最大垂直相机旋转角度重置为默认值。
}
// Update is called once per frame
void Update()
{
//随着鼠标移动旋转
// 让鼠标移动以绕相机运行。
//1.鼠标左右移动,摄像机也左右移动 围绕主角
//2.鼠标上下移动,摄像机也上下移动 围绕主角
// Mouse:
angleH += Mathf.Clamp(Input.GetAxis("Mouse X"), -1, 1) * 6f;
angleV += Mathf.Clamp(Input.GetAxis("Mouse Y"), -1, 1) * 6f;
// S等垂直移动限制。垂直的范围,上下的
angleV = Mathf.Clamp(angleV, minVerticalAngle, targetMaxVerticalAngle);
// 设置相机方向。转向要和获得鼠标的一致
Quaternion camYRotation = Quaternion.Euler(0, angleH, 0);
Quaternion aimRotation = Quaternion.Euler(-angleV, angleH, 0);
cam.rotation = aimRotation;
Vector3 noCollisionOffset = targetCamOffset;
// 没有自定义偏移的中间位置,请转到第一人称。
bool customOffsetCollision = isCustomOffset && noCollisionOffset.sqrMagnitude < targetCamOffset.sqrMagnitude;
// Repostition the camera.
smoothPivotOffset = Vector3.Lerp(smoothPivotOffset, customOffsetCollision ? pivotOffset : targetPivotOffset, smooth * Time.deltaTime);
smoothCamOffset = Vector3.Lerp(smoothCamOffset, customOffsetCollision ? Vector3.zero : noCollisionOffset, smooth * Time.deltaTime);
cam.position = player.position + camYRotation * smoothPivotOffset + aimRotation * smoothCamOffset;
}
// 将相机偏移量重置为默认值。
public void ResetTargetOffsets()
{
targetPivotOffset = pivotOffset;
targetCamOffset = camOffset;
isCustomOffset = false;
}
public void ResetMaxVerticalAngle()
{
this.targetMaxVerticalAngle = maxVerticalAngle;
}
// 将“视野”重置为默认值。
public void ResetFOV()
{
this.targetFOV = defaultFOV;
}
}
里面的些许代码解释就放到下一节了。晚安·晚安~~~~