[英]Rotating a player around the centre with touch controls
My player does revolve around the center object with this:我的播放器确实围绕中心 object 旋转:
private void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
}
This works on keyboard and I need it for touch controls.这适用于键盘,我需要它来进行触摸控制。 Like the player should move to left along the radius when i touch the left side of the screen and same for the right side.
就像当我触摸屏幕左侧时玩家应该沿着半径向左移动一样,右侧也是如此。 I'm having a hard time figuring out the controls for touch for this.
我很难弄清楚触摸的控件。 Any help would be appreciated.
任何帮助,将不胜感激。 Thank You.
谢谢你。
It seems that your actual question here would be看来您的实际问题是
How do I check if the User touches left or right half of the screen?
如何检查用户是否触摸了屏幕的左半部分或右半部分?
You can do this quite easily by comparing the touch.position.x
to the Screen.width / 2f
.您可以通过将
touch.position.x
与 Screen.width Screen.width / 2f
进行比较来轻松做到这一点。 If it is smaller than you are touching left, otherwise you are touching right.如果它小于您触摸左侧,否则您触摸右侧。
So you could probably do something like所以你可能会做类似的事情
[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);
}
}
For easier debugging / testing this you could actually combine it with mouse input like为了更轻松地调试/测试,您实际上可以将它与鼠标输入结合起来,例如
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.