[英]Rotating a player around the centre with touch controls
看来您的实际问题是
如何检查用户是否触摸了屏幕的左半部分或右半部分?
您可以通过将touch.position.x
与 Screen.width Screen.width / 2f
进行比较来轻松做到这一点。 如果它小于您触摸左侧,否则您触摸右侧。
所以你可能会做类似的事情
[SerializeField] private float moveSpeed = 45f;
void Update()
{
if(Input.touchCount > 0)
{
// Check whether the touch is on the left or right side of the screen
// so basically x is lower or higher than the screen center
var direction = Input.GetTouch(0).position.x < Screen.width / 2f ? 1 : -1;
// use the direction multiplier for the rotation direction
transform.RotateAround(Vector3.zero, Vector3.forward, moveSpeed * direction * Time.deltaTime);
}
}
为了更轻松地调试/测试,您实际上可以将它与鼠标输入结合起来,例如
void Update()
{
if(Input.touchSupported)
{
if(Input.touchCount > 0)
{
var direction = Input.GetTouch(0).position.x < Screen.width / 2f ? 1 : -1;
transform.RotateAround(Vector3.zero, Vector3.forward, moveSpeed * direction * Time.deltaTime);
}
}
else
{
if (Input.GetMouseButton(0))
{
var direction = Input.mousePosition.x < Screen.width / 2f ? 1 : -1;
transform.RotateAround(Vector3.zero, Vector3.forward, moveSpeed * direction * Time.deltaTime);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.