简体   繁体   中英

Rotating a player around the centre with touch controls

My player does revolve around the center object with this:

 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 . 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);
        }
    }
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM