关于魔兽视角的代码
2015年03月19日 11:05
0 点赞
0 评论
更新于 2025-11-21 17:40
代码功能概述
此相机脚本的主要功能是实现围绕 Y 轴的旋转和高度的平滑变化,同时保持相机与目标在 X - Z 平面上的水平距离固定。通过特定的平滑算法,我们可以对相机的行为进行精细控制。
代码详细分析
// This camera smoothes out rotation around the y-axis and height.
// Horizontal Distance to the target is always fixed.
// There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
// For every of those smoothed values we calculate the wanted value and the current value.
// Then we smooth it using the Lerp function.
// Then we apply the smoothed values to the transform's position.
// The target we are following
var GO_T: GameObject;
var target: Transform;
var camera1: Camera;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we
var heightDamping = 2.0;
var rotationDamping = 3.0;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate() {
// Early out if we don't have a target
if (!target) {
return;
}
// Adjust the distance based on mouse scroll wheel input
distance += Input.GetAxis("Mouse ScrollWheel") * 5;
// Clamp the distance value
if (distance > 10) {
distance = 10;
} else if (distance < 0) {
distance = 1;
}
// Adjust the height when the right mouse button is pressed
if (Input.GetKey(KeyCode.Mouse1)) {
height += Input.GetAxis("Mouse Y") * 5;
// Clamp the height value
if (height > 10) {
height = 10;
} else if (height < 0) {
height = 0;
}
}
// Make the camera look at the target
transform.LookAt(target);
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis using LerpAngle
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height using Lerp
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt(target);
// Raycast to check for walls
var hit: RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(GO_T.transform.position, -fwd, hit, 3)) {
if (hit.transform.tag == "wall") {
camera1.depth = 1;
camera1.enabled = true;
print("aaaaaa");
}
} else {
camera1.depth = 0;
camera1.enabled = false;
print("dddddddd");
}
}
代码解释
变量声明:
GO_T:一个游戏对象,用于射线检测。target:相机要跟随的目标的变换组件。camera1:关联的相机对象。distance:相机与目标在 X - Z 平面上的距离。height:相机相对于目标的高度。heightDamping和rotationDamping:分别用于控制高度和旋转的平滑程度。
LateUpdate函数:- 目标检查:如果没有指定目标,函数直接返回。
- 距离调整:通过鼠标滚轮输入调整相机与目标的距离,并将距离值限制在 0 到 10 之间。
- 高度调整:当按下鼠标右键时,通过鼠标的垂直移动调整相机的高度,并将高度值限制在 0 到 10 之间。
- 旋转和平滑:计算目标的旋转角度和高度,然后使用
Mathf.LerpAngle和Mathf.Lerp函数对当前的旋转角度和高度进行平滑处理。 - 位置设置:根据平滑后的旋转和高度值,设置相机的位置,并使其始终朝向目标。
- 射线检测:使用射线检测相机前方是否有标记为 “wall” 的物体。如果检测到墙,启用
camera1并设置其深度为 1;否则,禁用camera1并设置其深度为 0。
通过这种方式,我们可以实现一个具有平滑旋转和高度调整功能的相机,并且能够根据前方是否有墙来切换相机的显示状态。