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

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

This is the code I found:

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):

The code I wrote 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

• The * 40f / 18 are what we call "Magic Numbers" - so named because it's difficult for anyone who didn't write the code originally to infer why those particular values are appropriate for this job - they're just "magic". I would say, if you don't understand this code, do not use it. It will cause you problems down the line if you need to modify it but don't understand what you're modifying. Instead, try editing your question to describe the behaviour that you need, and ask about how to achieve it. Then we can give you new code to achieve that end, and explain to you how it does it. Dec 25, 2020 at 19:58
• @DMGregory Performed Dec 25, 2020 at 21:55
• This isn't enough for someone who hasn't played Cube Surfer to know exactly what you want, so take the time to describe it in detail. I'd expect multiple paragraphs worth here. Dec 25, 2020 at 22:21
• @DMGregory I edited Jan 4, 2021 at 7:42
• Do you want to move the same distance regardless of resolution (fixed speed of your player) or do you want to be able to reach from the left to the right in the same time regardless of how width it is? Jan 4, 2021 at 13:31

Try multiplying your offset by your aspect ratio: Screen.width / Screen.height.

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 * ((float) Screen.width / Screen.height) * Time.deltaTime;

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

transform.position = offset;
}
}


• This does not work Jan 6, 2021 at 7:14
• @AimonZ. "Does not work" is never enough information to diagnose a problem. Always include the specific symptoms you observe if you want the author to be able to use them to find improvements to their answer. Jan 6, 2021 at 14:06
• @AimonZ This should work (if it doesn't please let people know what went wrong or at least what the results look like) but the problem is mostly likely that Screen.width and Screen.height are ints. So 1440 / 2960 = 0. What you need to do is cast them to floats so you can preserve the decimal points offset.x += delta.x * Sensitivity * ((float)Screen.width / Screen.height) * Time.deltaTime; Jan 7, 2021 at 7:30
• @BenjaminDangerJohnson good catch. Updated answer
– Evorlor
Jan 7, 2021 at 18:51

You are using Mathf.Clamp to prevent it from going out of bounds. You can use it as well to restrict the speed

offset.x += Mathf.Clamp(delta.x * Sensitivity, -_MaxSpeed, _MaxSpeed) * Time.deltaTime;


_MaxSpeed is something you would need to figure out until it matches your gameplay.

• 4. I do not use the screen joystick. Dec 25, 2020 at 17:36