How to make the character move along the axis using touch?

Viewed 787

I have the following code it works, but I don't know how it works.

void Update()
{
#if UNITY_ANDROID

    if(Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Moved )
    {
        Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
        float offset = touchDeltaPosition.x * 40f / 18 * Mathf.Deg2Rad;
        transform.position += new Vector3(0f, 0f, offset);
        transform.position = new Vector3(0f, 0f, Mathf.Clamp(transform.position.z, -7.1f, 7.1f));
    }     
#endif   
}

I need to move the player along the axis by touch, for example, like in the game Cube Surfer!

What I want to get (I used android emulator):

enter image description here

The code I wrote based on the answers below works well at 720x1280, but if you set the resolution to 1440x2960, the controls become too sharp. I know why this is happening because touch.delta is getting too big.

My code:

if (Input.touchCount > 0)
{
    var t = Input.GetTouch(0);

    var delta = t.deltaPosition;

    if (delta != Vector2.zero)
    {
        var offset = transform.position;

        offset.x += delta.x * Sensitivity * Time.deltaTime;

        offset.x = Mathf.Clamp(offset.x, -4f, 4f);

        transform.position = offset;
    }
}

How to fix it?

ScreenToWorldPoint this does not fit because the player moves along a spline.

Thank for help

4 Answers

I will suggest a code that is way simpler and it is easy to understand

if (Input.touchCount > 0)
    {
        touch = Input.GetTouch(0);
        if (touch.phase == TouchPhase.Moved)
        {
            transform.position = new Vector3(
               transform.position.x + touch.deltaPosition.x * speedModifier,
               transform.position.y,
               transform.position.z);
        }
    }
  • With Input.touchCount > 0 you are checking is there are touches
  • With touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Moved) you are checking if touch is moving or dragging
  • And the last part is just the movement along the axis that I want, in my case only the x axis, where speedModifier is the speed of the movement.

If you want to try it on your computer you need to do a similar thing with raycast, but if you plug your phone yo unity it will work like this.

You can easily move it along other axis by simply adding + touch.deltaPosition.x * speedModifier

Use this Code

Each Frame we are calculating the delta move of your current touch and multiply it by the speed (during a time frame that's the reason of multiplying by Time.deltaTime )

    using System.Collections.Generic;
    using UnityEngine;
 
     public class BasicMovement : MonoBehaviour {

     public float speed=5;
 
     void Update () { 
        if(Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Moved )
        {
           Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
           float offset = touchDeltaPosition.x * speed * Time.deltaTime;
           transform.position = new Vector3(0f, 0f, Mathf.Clamp(offset , -5f,5f));
        }

     }
 }
float worldWidth         = 14f;  //serialized, example value
float ratioScreenToWorld = 2.0f; //serialized, example value

float screenWidth        = (float) Screen.width;

if (Input.touchCount > 0)
{
    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Moved)
    {
        float horizontal = touch.deltaPosition.x;
        transform.position +=
            new Vector3(0f, 0f, horizontal * (worldWidth / screenWidth) * ratioScreenToWorld);
        //clamp, or whatever
    }
}

The root of your woes is that you're not normalizing the movement of the finger to some standard.

With ratioScreenToWorld = 1f, this code maps an offset of the finger on the screen to an offset of the object on the path. The width of the path is specified by worldWidth. Upon changing ratioScreenToWorld it re-proportionates that mapping, e.g. with ratioScreenToWorld = 0.5f in layman terms, "moving your finger across the full screen moves the object on the road/path by half the width of the path".

Each Frame we are calculating the delta move of your current touch and multiply it by the speed (during a time frame that's the reason of multiplying by Time.deltaTime )

Related