[英]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.