gpt4 book ai didi

c# - 放大Unity时如何使RTS相机平滑

转载 作者:行者123 更新时间:2023-11-30 17:27:09 25 4
gpt4 key购买 nike

现在,我正在尝试使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和Google应该提供。

我要做的第二件事是以我可以管理的最直接方式解决您的问题,然后,我们将看看是否无法提出一种更好的方法来解决您的问题。

因此,关键是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()作为替代的插值方法。)最后,我们只是在地面附近选择了一个点,而不是直接摆弄相机的旋转值。将相机直接对准那个位置。

通过保持相机的位置和方向彼此完全独立,并分离出相机高度的“动画”,我们避免了很多麻烦,并消除了许多杂乱交织的错误的可能性。

希望对您有所帮助。

关于c# - 放大Unity时如何使RTS相机平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55458660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com