现在我正试图让rts相机在靠近地面时进行平移。我现在遇到的问题是我使用鼠标滚轮进行缩放,这使得缩放感觉就像它的滞后一样。看起来它会跳过一些Y值并传送而不是平稳地移动到所需的位置。此外,我想知道如何使相机停在最小Y值,因为现在发生的是它停在22左右而不是20,这是我相机运动的最小Y值。
我尝试使用+和 - 在我的小键盘上进行缩放,它按照我想要的方式工作(平滑地放大和缩小而不跳过),但并非所有玩家都有小键盘,我觉得它更适合用鼠标滚轮进行缩放。
{
private const int levelArea = 100;
private const int scrollArea = 25;
private const int scrollSpeed = 25;
private const int dragSpeed = 70;
private const int zoomSpeed = 50;
// Maximum/minimum zoom distance from the ground
public int zoomMin = 20;
public int zoomMax = 120;
private const int panSpeed = 40;
// Minimal/maximal angles for camera
private const int panAngleMin = 30;
private const int panAngleMax = 90;
void Update()
{
// Init camera translation for this frame.
var translation = Vector3.zero;
// Zoom in or out
var zoomDelta = Input.GetAxis("Mouse ScrollWheel") * zoomSpeed * Time.deltaTime;
if (zoomDelta != 0)
{
translation -= Vector3.up * zoomSpeed * zoomDelta;
}
// Start panning camera if zooming in close to the ground or if just zooming out.
var pan = transform.eulerAngles.x - zoomDelta * panSpeed;
pan = Mathf.Clamp(pan, panAngleMin, panAngleMax);
// When to start panning up the camera
if (zoomDelta < 0 || transform.position.y < (zoomMax -20))
{
transform.eulerAngles = new Vector3(pan, 0, 0);
}
// Move camera with arrow keys
translation += new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
// Move camera with mouse
if (Input.GetMouseButton(2)) // MMB
{
// Hold button and drag camera around
translation -= new Vector3(Input.GetAxis("Mouse X") * dragSpeed * Time.deltaTime, 0,
Input.GetAxis("Mouse Y") * dragSpeed * Time.deltaTime);
}
else
{
// Move camera if mouse pointer reaches screen borders
if (Input.mousePosition.x < scrollArea)
{
translation += Vector3.right * -scrollSpeed * Time.deltaTime;
}
if (Input.mousePosition.x >= Screen.width - scrollArea)
{
translation += Vector3.right * scrollSpeed * Time.deltaTime;
}
if (Input.mousePosition.y < scrollArea)
{
translation += Vector3.forward * -scrollSpeed * Time.deltaTime;
}
if (Input.mousePosition.y > Screen.height - scrollArea)
{
translation += Vector3.forward * scrollSpeed * Time.deltaTime;
}
}
// Keep camera within level and zoom area
var desiredPosition = transform.position + translation;
if (desiredPosition.x < -levelArea || levelArea < desiredPosition.x)
{
translation.x = 0;
}
if (desiredPosition.y < zoomMin || zoomMax < desiredPosition.y)
{
translation.y = 0;
}
if (desiredPosition.z < -levelArea || levelArea < desiredPosition.z)
{
translation.z = 0;
}
// Move camera parallel to world axis
transform.position += translation;
}
}
我希望从相机现在的位置和滚动进/出后的所需位置平滑过渡。而且我想知道如何使相机停在最小/最大变焦距离而不是靠近它。谢谢你的帮忙。视频如何移动相机:https://youtu.be/Lt3atJEaOjA
好的,所以我要在这里做三件事。首先,我建议如果你正在使用高级相机行为,你可能想要至少考虑使用Cinemachine。我自己会引导你完成它,但鉴于我缺乏个人经验,即使尝试,我也可能会伤害你。那里有很多很好的教程。 Youtube和谷歌应该提供。
我要做的第二件事是以最直接的方式解决你的问题,然后,我们会看到我们是否能找到更好的方法来解决你的问题。
所以,这里的关键是Unity的滚轮输入非常二进制。检查滚轮轴时,结果直接取决于自上次帧更新后您的车轮经过了多少次“点击”,但您真正想要的是一些给予的东西。默认情况下,Unity实际上可以使用其大多数轴输入执行此操作:您可能会注意到,如果您在默认的Unity项目中使用WASD,那么它会有一些“滞后”,您可以将手指从键上移开但是你仍将继续从Input.GetAxis()
获得几帧的正值。这与输入设置中的Gravity
值相关联,而Input.GetAxisRaw()
实际上用于完全绕过它。无论出于何种原因,滚轮轴似乎不受轴重力的影响,因此我们基本上必须实现类似的东西。
// Add this property to your class definition (so it persists between updates):
private float wheelAxis = 0;
// Delete this line:
var zoomDelta = Input.GetAxis("Mouse ScrollWheel") * zoomSpeed * Time.deltaTime;
// And put these three new lines in its place:
wheelAxis += Input.GetAxis("Mouse ScrollWheel");
wheelAxis = Mathf.MoveTowards(wheelTotal, 0f, Time.deltaTime);
var zoomDelta = Mathf.Clamp(wheelAxis, -0.05f, 0.05f) * zoomSpeed * Time.deltaTime;
是的,所以我们在这里做了一些事情。每次更新,我们都会将当前的滚轮值添加到我们的wheelAxis
中。接下来,我们通过Time.deltatime
函数将当前的Mathf.MoveTowards()
应用为“重力”。最后,我们通过一个简单的修改来调用大部分只是旧的zoomDelta
代码:我们用wheelAxis
约束Mathf.Clamp
来尝试调节缩放的速度。
您可以通过几种方式修改此代码。如果乘以Time.deltaTime
参数,则可以影响输入“持续”的时间长度。如果你搞乱Mathf.Clamp()
值,你会变得更快或更慢。总而言之,如果您只是希望在代码变化最小的情况下进行平滑缩放,那么这可能是您最好的选择。
所以!
现在我们已经完成了这个,让我们谈谈你的代码,以及你如何解决问题,看看我们是否找不到更清洁的解决方案。
获得良好的相机工作是非常重要的。您的代码看起来像我看到的许多代码试图解决一个复杂的问题:看起来您添加了一些功能,然后对其进行了测试,并发现了一些边缘情况,它分崩离析,然后修补了这些情况,然后尝试了在旧代码的基础上实现一个新功能,但它有其他各种方式,等等,我们在这一点上得到的有点混乱。
您的代码最大的问题是相机的位置和相机的旋转紧密相连。使用角色时,这通常很好,但在使用相机时,你想要解决这个问题。想想相机的位置以及相机所看到的东西,作为非常独立的东西来跟踪。
所以这是一个工作相机脚本,您应该能够插入并运行:
using UnityEngine;
public class RTSCamera : MonoBehaviour
{
public float zoomSpeed = 100f;
public float zoomTime = 0.1f;
public float maxHeight = 100f;
public float minHeight = 20f;
public float focusHeight = 10f;
public float focusDistance = 20f;
public int panBorder = 25;
public float dragPanSpeed = 25f;
public float edgePanSpeed = 25f;
public float keyPanSpeed = 25f;
private float zoomVelocity = 0f;
private float targetHeight;
void Start()
{
// Start zoomed out
targetHeight = maxHeight;
}
void Update()
{
var newPosition = transform.position;
// First, calculate the height we want the camera to be at
targetHeight += Input.GetAxis("Mouse ScrollWheel") * zoomSpeed * -1f;
targetHeight = Mathf.Clamp(targetHeight, minHeight, maxHeight);
// Then, interpolate smoothly towards that height
newPosition.y = Mathf.SmoothDamp(transform.position.y, targetHeight, ref zoomVelocity, zoomTime);
// Always pan the camera using the keys
var pan = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")) * keyPanSpeed * Time.deltaTime;
// Optionally pan the camera by either dragging with middle mouse or when the cursor touches the screen border
if (Input.GetMouseButton(2)) {
pan -= new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")) * dragPanSpeed * Time.deltaTime;
} else {
var border = Vector2.zero;
if (Input.mousePosition.x < panBorder) border.x -= 1f;
if (Input.mousePosition.x >= Screen.width - panBorder) border.x += 1f;
if (Input.mousePosition.y < panBorder) border.y -= 1f;
if (Input.mousePosition.y > Screen.height - panBorder) border.y += 1f;
pan += border * edgePanSpeed * Time.deltaTime;
}
newPosition.x += pan.x;
newPosition.z += pan.y;
var focusPosition = new Vector3(newPosition.x, focusHeight, newPosition.z + focusDistance);
transform.position = newPosition;
transform.LookAt(focusPosition);
}
}
虽然我鼓励你在自己的时间里完成它,但我不会把你拖过每一寸。相反,我只是回顾主要症结。
这里的关键思想是,不是直接控制相机的高度和方向,而是让滚轮指示相机高度的位置,然后我们使用Mathf.SmoothDamp()
将相机平滑地移动到几个帧的那个位置。 (Unity有许多这样的有用函数。考虑使用Mathf.MoveTowards()
作为替代插值方法。)最后,我们只是在靠近地面的地方选择一个点,然后指向它,而不是试图直接摆弄相机的旋转值。直接在那个地方的相机。
通过保持相机的位置和方向完全相互独立,以及分离相机高度的“动画”,我们避免了很多麻烦,并消除了很多杂乱交织的错误的可能性。
我希望有所帮助。