英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论