C# Unity相机上下移动速度非常慢

huangapple go评论49阅读模式
英文:

C# Unity camera moving up and down speed is very slow

问题

以下是代码的翻译部分:

if (Input.GetMouseButtonDown(0))
{
    isDragged = true;
    touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
else if (Input.GetMouseButtonUp(0))
{
    isDragged = false;
}
if (Input.GetMouseButton(0) && isDragged == true)
{
    Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
    direction.y = 0;

    Vector3 newPosition = Camera.main.transform.position + direction;
    newPosition.x = Mathf.Clamp(newPosition.x, -45, 45);
    newPosition.z = Mathf.Clamp(newPosition.z, -45, 45);

    Camera.main.transform.position = newPosition;
}

对于相机上下左右速度不一致的问题,你尝试添加相机加速度,但没有成功。速度仍然不同。

英文:

The project uses the main camera with orthographic mode and rotation of 45 and 30 degrees in X and Y. How can I change the code so that the camera moves at the same speed up/down and left/right? In the current version, due to changing only two coordinates, I cannot get the same speed (the camera moves up and down very slowly). The Y coordinate does not change because this is the height and because of it it is not possible to correctly limit the camera.

if (Input.GetMouseButtonDown(0))
{
    isDragged = true;
    touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
else if (Input.GetMouseButtonUp(0))
{
    isDragged = false;
}
if (Input.GetMouseButton(0) && isDragged == true)
{
    Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
    direction.y = 0;

    Vector3 newPosition = Camera.main.transform.position + direction;
    newPosition.x = Mathf.Clamp(newPosition.x, -45, 45);
    newPosition.z = Mathf.Clamp(newPosition.z, -45, 45);

    Camera.main.transform.position = newPosition;
}

I tried to add camera acceleration up and down, but it did not work. The speed is still different.

答案1

得分: 0

我通过将Y坐标添加到X和Z来解决了这个问题:

direction.x += direction.y;
direction.z += direction.y;
direction.y = 0;
英文:

I solved the problem by adding the Y coordinate to X and Z:

    direction.x += direction.y;
    direction.z += direction.y;
    direction.y = 0;

huangapple
  • 本文由 发表于 2023年2月26日 20:16:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定