1
\$\begingroup\$

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

enter image description here

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

\$\endgroup\$
6
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Dec 25, 2020 at 19:58
  • \$\begingroup\$ @DMGregory Performed \$\endgroup\$
    – Aimon Z.
    Dec 25, 2020 at 21:55
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Dec 25, 2020 at 22:21
  • \$\begingroup\$ @DMGregory I edited \$\endgroup\$
    – Aimon Z.
    Jan 4, 2021 at 7:42
  • \$\begingroup\$ 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? \$\endgroup\$
    – Zibelas
    Jan 4, 2021 at 13:31

2 Answers 2

1
\$\begingroup\$

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

Note: you may need to adjust your sensitivity.

\$\endgroup\$
4
  • \$\begingroup\$ This does not work \$\endgroup\$
    – Aimon Z.
    Jan 6, 2021 at 7:14
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – DMGregory
    Jan 6, 2021 at 14:06
  • 2
    \$\begingroup\$ @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; \$\endgroup\$ Jan 7, 2021 at 7:30
  • 1
    \$\begingroup\$ @BenjaminDangerJohnson good catch. Updated answer \$\endgroup\$
    – Evorlor
    Jan 7, 2021 at 18:51
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 4. I do not use the screen joystick. \$\endgroup\$
    – Aimon Z.
    Dec 25, 2020 at 17:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .