StuckInPhDNoMore
StuckInPhDNoMore

Reputation: 2679

How to constantly get the touch position?

I have a code that predicts a shooting path of an arrow in LateUpdate(). It is working perfectly on PC and with mouse. Below is the code for when using a mouse:

    if(Input.GetMouseButton(0)){
        //get the rotation based on the start drag position compared to the current drag position
        zRotation = (Input.mousePosition.y - mouseStart) * (manager.data.sensitivity/15);
        zRotation = Mathf.Clamp(zRotation, -manager.data.maxAimRotation, manager.data.maxAimRotation);
    }
    //reset the rotation if the player is not aiming
    else if((int) zRotation != 0){
        if(zRotation > 0){
            zRotation --;
        }
        else{
            zRotation ++;
        }
    }

I want to port this to Android now so am playing around with Input.Touch. I changed the above code to the following:

 if (Input.touchCount > 0)
            {
                //get the rotation based on the start drag position compared to the current drag position
                zRotation = (Input.GetTouch(0).deltaPosition.y) * (manager.data.sensitivity / 15);
                zRotation = Mathf.Clamp(zRotation, -manager.data.maxAimRotation, manager.data.maxAimRotation);
            }
            //reset the rotation if the player is not aiming
            else if ((int)zRotation != 0)
            {
                if (zRotation > 0)
                {
                    zRotation--;
                }
                else
                {
                    zRotation++;
                }
            }

But the zRotation does not work as it works in mouse. It keeps resetting to the start position after every frame. It almost looks like jittering.

What am I doing wrong?

Upvotes: 0

Views: 656

Answers (1)

Noodles
Noodles

Reputation: 4080

I see that for mobile controls you are using Input.GetTouch(0).deltaPosition.y. However, deltaPosition gives you the difference between the position on the last update compared to the current one. So let's say your app is running at 60 frames per second, it will return the distance per 1/60th of a second. Of course this will be a number close to zero and I believe that is why it looks like it always returns the starting position

https://docs.unity3d.com/ScriptReference/Touch-deltaPosition.html

You will have to do it similarly to how you did it with your mouse approach. Set a variable touchStart on Touchphase.Began and compare it to touch.position.

float touchStart = 0;

if (Input.touchCount > 0)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Began) touchStart = Input.GetTouch(0).position.y;
                //get the rotation based on the start drag position compared to the current drag position
                zRotation = (Input.GetTouch(0).position.y - touchStart) * (manager.data.sensitivity / 15);
                zRotation = Mathf.Clamp(zRotation, -manager.data.maxAimRotation, manager.data.maxAimRotation);
            }
            //reset the rotation if the player is not aiming
            else if ((int)zRotation != 0)
            {
                if (zRotation > 0)
                {
                    zRotation--;
                }
                else
                {
                    zRotation++;
                }
            }

I haven't tested this though, so correct me if I'm wrong!

Upvotes: 1

Related Questions