我试图用鼠标在屏幕上移动十字准线,但始终将十字准线保持在静态定位的相机的视野中。我下面的脚本通常有效,将十字准线保持在屏幕的左侧,右侧和底部。问题是我仍然可以将十字准线向上移动到屏幕上方。感觉它对此有一定的抵抗力,这很奇怪,但是它比屏幕范围更高,而不是锁定在0.95。
相关信息:
相机上的-10度X旋转(可能是问题,但视口不应该知道吗?)
void Update()
{
if (Input.GetMouseButtonDown(0))
{
// Capture touch origin to create virtual joystick center
firstTouchPos = Camera.main.ScreenPointToRay(Input.mousePosition);
crosshairPos = crosshair.position;
}
if (Input.GetMouseButton(0))
{
// Get mouse position
Ray currentTouchPos = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
// Move crosshair
targetPosition = crosshairPos + direction * touchSensitivity;
Vector3 pos = Camera.main.WorldToViewportPoint(targetPosition);
pos.x = Mathf.Clamp(pos.x, 0.05f, 0.95f);
pos.y = Mathf.Clamp(pos.y, 0.05f, 0.95f);
targetPosition = Camera.main.ViewportToWorldPoint(pos);
crosshair.position = Vector3.Lerp(crosshair.position, new Vector3(targetPosition.x, targetPosition.y, -0.6f), Time.deltaTime * followSpeed);
}
}