【发布时间】:2019-05-14 15:13:01
【问题描述】:
我正在使用鼠标水平控制一个游戏对象。对象根据鼠标位置向左或向右移动,但一旦鼠标停止移动,对象也会停止。我想要的是,如果我将鼠标拖动到屏幕的右侧并停止移动鼠标,那么只要鼠标在屏幕的右半部分,我希望对象向右移动,反之亦然。当前 Input.GetAxis("鼠标 X");停止时返回 0 值。代码如下:
float horizontalInput = Input.GetAxis("Mouse X");
我已经尝试使用此修复程序,但我没有得到想要的结果,因为我使用 mousebuttondown 将我的播放器向前移动。
if (Input.GetMouseButton(0) && (horizontalInput == 0f))
{
if (Input.mousePosition.x < Screen.width / 2)
{
horizontalInput = -1f;
}
else if (Input.mousePosition.x > Screen.width / 2)
{
horizontalInput = 1f;
}
}
还有其他方法可以实现吗?非常感谢您的宝贵时间!
我通过调整上面的 if 条件来修复它!
if (Input.GetMouseButton(0))
{
if (Input.mousePosition.x < Screen.width / 2 && (horizontalInput < 0f))
{
horizontalInput = -1f;
}
if (Input.mousePosition.x > Screen.width / 2 && (horizontalInput > 0f))
{
horizontalInput = 1f;
}
}
【问题讨论】:
-
Input.mousePosition?
-
你的意思是像 if(Input.mousePosition.x
-
我通过更改 if 条件来检查左右移动的水平输入是大于还是小于 0 来修复它!