5

I am trying to create a player movement along the x-axis based on finger position.

What I need to Happen: Not multi-Touch. I want it so a player can put a single finger down and grab that position. Then check to see if the player dragged finger along screen on x-axis and move player left or right based off where they dragged finger from first touch.

So if they touch the screen and if drag left: move left by speed and if it changes to drag right move right by speed.

Any help would be Awesome.

shane
  • 342
  • 1
  • 5
  • 28
  • please show us your research effort, what have you already tried and post your current relevant code – Jinjinov Jul 24 '18 at 08:39

2 Answers2

8

The easiest way would be to stored first touch position and then compare the X with that position:

public class PlayerMover : MonoBehaviour
{
    /// Movement speed units per second
    [SerializeField]
    private float speed;

    /// X coordinate of the initial press
    // The '?' makes the float nullable
    private float? pressX;



    /// Called once every frame
    private void Update()
    {
        // If pressed with one finger
        if(Input.GetMouseButtonDown(0))
            pressX = Input.touches[0].position.x;
        else if (Input.GetMouseButtonUp(0))
            pressX = null;


        if(pressX != null)
        {
            float currentX = Input.touches[0].position.x;

            // The finger of initial press is now left of the press position
            if(currentX < pressX)
                Move(-speed);

            // The finger of initial press is now right of the press position
            else if(currentX > pressX)
                Move(speed);

            // else is not required as if you manage (somehow)
            // move you finger back to initial X coordinate
            // you should just be staying still
        }
    }


    `
    /// Moves the player
    private void Move(float velocity)
    {
        transform.position += Vector3.right * velocity * Time.deltaTime;
    }

}

WARNING: this solution will only work for devices with touch input available (because of Input.touches use).

tsvedas
  • 1,049
  • 7
  • 18
  • Could you tell me what the ? after float is called or a link to understand that better? Everything works fine on device For anyone else looking, you just cant run it in editor. – shane Jul 26 '18 at 20:22
  • I wrote a warning at the bottom of the answer, saying that this works only in devices with touch input. As for reference on '?', see https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/index#nullable-types-overview („The syntax T? is shorthand for Nullable, where T is a value type. The two forms are interchangeable.“) – tsvedas Jul 27 '18 at 06:21
2

using the code @programmer provided in this answer: Detect swipe gesture direction

You can easily detect the direction you are swiping/dragging. Replace the Debug

 void OnSwipeLeft()
{
    Debug.Log("Swipe Left");
}

void OnSwipeRight()
{
    Debug.Log("Swipe Right");
}

With functions that move your character. If you use RigidBody to move your character you can use https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html. If its a normal object, you can move it by tweaking the transform.position.

If you need more information on how to move rigidbody\normal objects let me know what type of game you have and more details on how you set up the player.

SpoocyCrep
  • 604
  • 7
  • 23