Screen.width 似乎是从屏幕中心扫射,而不是全屏时相对于屏幕大小
Screen.width seems to be strafing from the center of the screen instead of relative to the screen size on full screen
我有一个 C# 脚本来控制我正在开发的塔防游戏的自上而下视角的主摄像头。据我所知,Screen.width 应该随屏幕尺寸缩放,因此使用 Screen.width - 所需的边框厚度作为相机移动的限定符应该从屏幕边框厚度边缘的指定位置触发方向。
我遇到的问题是,无论屏幕大小如何,将鼠标移到屏幕的中间点上并向右移动都会导致相机向该方向移动。这是 strafing right 独有的 - left 行为与使用相同代码的预期一样。
我猜左侧有效是因为屏幕像素数从左下角开始,并且出于某种原因 Screen.width 限制它的缩放比例相对于 Screen.height,但是我找不到其他人 运行 在 3d 项目中遇到同样的问题,这让我觉得我忽略了一些愚蠢的事情。我看到一些关于正交相机应用于 2d 项目的东西,但我想我会在这里拍摄一些东西,以防有人突然意识到发生了什么。
这是我的代码:
using UnityEngine;
public class CameraController : MonoBehaviour {
private bool doMovement = true;
public float panSpeed = 20f;
public float panBorderThickness = 10f;
public float scrollSpeed = 5f;
public float minY = 40f;
public float maxY = 120f;
void Update () {
if (GameManager.gameIsOver)
{
this.enabled = false;
return;
}
if (Input.GetKeyDown(KeyCode.Escape))
doMovement = !doMovement;
if (!doMovement)
return;
if (Input.GetKey("d") || Input.mousePosition.x >= Screen.height - panBorderThickness)
{
transform.Translate(Vector3.forward * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("a") || Input.mousePosition.x <= panBorderThickness)
{
transform.Translate(Vector3.back * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("s") || Input.mousePosition.y <= panBorderThickness)
{
transform.Translate(Vector3.right * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("w") || Input.mousePosition.y >= Screen.width - panBorderThickness)
{
transform.Translate(Vector3.left * panSpeed * Time.deltaTime, Space.World);
}
float scroll = Input.GetAxis("Mouse ScrollWheel");
Vector3 pos = transform.position;
pos.y -= scroll * 1000 * scrollSpeed * Time.deltaTime;
pos.y = Mathf.Clamp(pos.y, minY, maxY);
transform.position = pos;
}
}
使用
Screen.height-Input.mousePosition.y
屏幕位置和输入的y是相反的,不是你的左边右边是你的上下。
我有一个 C# 脚本来控制我正在开发的塔防游戏的自上而下视角的主摄像头。据我所知,Screen.width 应该随屏幕尺寸缩放,因此使用 Screen.width - 所需的边框厚度作为相机移动的限定符应该从屏幕边框厚度边缘的指定位置触发方向。
我遇到的问题是,无论屏幕大小如何,将鼠标移到屏幕的中间点上并向右移动都会导致相机向该方向移动。这是 strafing right 独有的 - left 行为与使用相同代码的预期一样。
我猜左侧有效是因为屏幕像素数从左下角开始,并且出于某种原因 Screen.width 限制它的缩放比例相对于 Screen.height,但是我找不到其他人 运行 在 3d 项目中遇到同样的问题,这让我觉得我忽略了一些愚蠢的事情。我看到一些关于正交相机应用于 2d 项目的东西,但我想我会在这里拍摄一些东西,以防有人突然意识到发生了什么。
这是我的代码:
using UnityEngine;
public class CameraController : MonoBehaviour {
private bool doMovement = true;
public float panSpeed = 20f;
public float panBorderThickness = 10f;
public float scrollSpeed = 5f;
public float minY = 40f;
public float maxY = 120f;
void Update () {
if (GameManager.gameIsOver)
{
this.enabled = false;
return;
}
if (Input.GetKeyDown(KeyCode.Escape))
doMovement = !doMovement;
if (!doMovement)
return;
if (Input.GetKey("d") || Input.mousePosition.x >= Screen.height - panBorderThickness)
{
transform.Translate(Vector3.forward * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("a") || Input.mousePosition.x <= panBorderThickness)
{
transform.Translate(Vector3.back * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("s") || Input.mousePosition.y <= panBorderThickness)
{
transform.Translate(Vector3.right * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("w") || Input.mousePosition.y >= Screen.width - panBorderThickness)
{
transform.Translate(Vector3.left * panSpeed * Time.deltaTime, Space.World);
}
float scroll = Input.GetAxis("Mouse ScrollWheel");
Vector3 pos = transform.position;
pos.y -= scroll * 1000 * scrollSpeed * Time.deltaTime;
pos.y = Mathf.Clamp(pos.y, minY, maxY);
transform.position = pos;
}
}
使用
Screen.height-Input.mousePosition.y
屏幕位置和输入的y是相反的,不是你的左边右边是你的上下。