我正在通过以下方式对Update
事件进行摄像机导航(运动/旋转):
void UpdateMovement()
{
bool accelerate = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
moveDirection =
new
Vector3(
Input.GetAxisRaw("Horizontal") * moveSpeed,
0,
Input.GetAxisRaw("Vertical") * moveSpeed);
//moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, 0, Input.GetAxis("Vertical") * moveSpeed);
moveDirection = transform.TransformDirection(moveDirection);
if (Input.GetButton("Up"))
{
moveDirection.y += moveSpeed;
}
else if (Input.GetButton("Down"))
{
moveDirection.y -= moveSpeed;
}
if (Input.GetAxisRaw("Mouse ScrollWheel") > 0)
{
moveDirection.y = moveDirection.y + scrollSpeed;
}
else if (Input.GetAxisRaw("Mouse ScrollWheel") < 0)
{
moveDirection.y = moveDirection.y - scrollSpeed;
}
moveDirection *= (accelerate ? speed : moveSpeed);
controller.Move(moveDirection * Time.deltaTime);
}
void UpdateRotation()
{
if (!Input.GetMouseButton(1))
return;
rotationX += Input.GetAxis("Mouse X") * lookSpeed;
rotationY += Input.GetAxis("Mouse Y") * lookSpeed;
rotationY = Mathf.Clamp(rotationY, -90, 90);
rotationZ = Input.GetAxis("Mouse ScrollWheel");
transform.localRotation = Quaternion.AngleAxis(rotationZ, Vector3.forward);
transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
}
一切正常,但是当我使用鼠标旋转相机并超出WebGl画布的范围时,WebGL画布上的问题同时出现,我也连续按水平或垂直键,然后释放输入键不起作用。记得我记录了键[Input.GetAxisRaw("Horizontal")
,Input.GetAxisRaw("Vertical")
]的输入,发现它在释放时并未重置为零。
Debug.Log("Hr GetAxisRaw : " + Input.GetAxisRaw("Horizontal"));
Debug.Log("Vertical : " + Input.GetAxisRaw("Vertical"));
通常,当我不使用鼠标旋转相机时,在此期间,当我释放水平/垂直键时,它可以正常工作。我以前使用的是Input.GetAxis
,现在我使用的是Input.GetAxisRaw
,但问题是相同的。