Touch input on mobile device

JtBing

I have this script for inputting touches on the mobile device. But it activates only once, I need it to be run until I take my finger off the screen

public float speed = 3;


public Rigidbody rb;
public void MoveLeft()
{
    transform.Translate(-Vector3.right * speed * Time.deltaTime);
}
public void StopMoving()
{
    rb.velocity = new Vector2(0, 0);
}

public void MoveRight()
{
    rb.velocity = new Vector2(-speed, 0);
}
private void Update()
{
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        float middle = Screen.width / 2;
        if (touch.position.x < middle && touch.phase == TouchPhase.Began)
        {
            MoveLeft();
        }
        else if (touch.position.x > middle && touch.phase == TouchPhase.Began )
        {
            MoveRight();
        }
    }
    else
    {
        StopMoving();
    }
}

}

ryeMoss

You simply need to remove the two && touch.phase == TouchPhase.Began portions. This will have the overall if-statement evaluate true as long as there is one finger on the screen.

private void Update()
{
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        float middle = Screen.width / 2;
        if (touch.position.x < middle)
        {
            MoveLeft();
        }
        else if (touch.position.x > middle)
        {
            MoveRight();
        }
    }
    else
    {
        StopMoving();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Controlling a player using touch input on the mobile device

From Dev

Controlling a player using touch input on the mobile device

From Dev

jquery append class with hover AND touch for mobile device

From Dev

Android device as touch input for Windows 8

From Dev

ng-click and ng-touch mobile device

From Dev

Do we have a mouse + touch pad input device for PC?

From Dev

Can't get touch input from eGalax device to QtQuick application

From Dev

jQuery HTML - mobile device: onclick not working as long as input is active

From Dev

HTML number input field that also works on mobile device

From Dev

How to stop touch event from falling through on overlay object in web browser for mobile device?

From Dev

How to stop touch event from falling through on overlay object in web browser for mobile device?

From Dev

If Device Supports Touch ID

From Dev

Pan viewport on touch device

From Dev

Mobile touch behaviour

From Dev

onClick not working on mobile (touch)

From Dev

Submenu not showing in mobile touch

From Dev

Mobile Safari - Input caret does not scroll along with overflow-scrolling: touch

From Dev

Mobile Safari - Input caret does not scroll along with overflow-scrolling: touch

From Dev

Capture and stop dragging on a touch device

From Dev

How to autostart an application on Touch device?

From Dev

How to Have Drupal WebForm phone number input show a keypad on a mobile device

From Dev

Mobile device detection on Tumblr

From Dev

ionicGestures not working in Mobile Device

From Dev

PHP in Javascript if mobile device

From Dev

Site on Mobile Device

From Dev

Fixed background on mobile device

From Dev

ionicGestures not working in Mobile Device

From Dev

Fontastic not loading on mobile device

From Dev

Implement mobile touch keypad in javascript

Related Related

  1. 1

    Controlling a player using touch input on the mobile device

  2. 2

    Controlling a player using touch input on the mobile device

  3. 3

    jquery append class with hover AND touch for mobile device

  4. 4

    Android device as touch input for Windows 8

  5. 5

    ng-click and ng-touch mobile device

  6. 6

    Do we have a mouse + touch pad input device for PC?

  7. 7

    Can't get touch input from eGalax device to QtQuick application

  8. 8

    jQuery HTML - mobile device: onclick not working as long as input is active

  9. 9

    HTML number input field that also works on mobile device

  10. 10

    How to stop touch event from falling through on overlay object in web browser for mobile device?

  11. 11

    How to stop touch event from falling through on overlay object in web browser for mobile device?

  12. 12

    If Device Supports Touch ID

  13. 13

    Pan viewport on touch device

  14. 14

    Mobile touch behaviour

  15. 15

    onClick not working on mobile (touch)

  16. 16

    Submenu not showing in mobile touch

  17. 17

    Mobile Safari - Input caret does not scroll along with overflow-scrolling: touch

  18. 18

    Mobile Safari - Input caret does not scroll along with overflow-scrolling: touch

  19. 19

    Capture and stop dragging on a touch device

  20. 20

    How to autostart an application on Touch device?

  21. 21

    How to Have Drupal WebForm phone number input show a keypad on a mobile device

  22. 22

    Mobile device detection on Tumblr

  23. 23

    ionicGestures not working in Mobile Device

  24. 24

    PHP in Javascript if mobile device

  25. 25

    Site on Mobile Device

  26. 26

    Fixed background on mobile device

  27. 27

    ionicGestures not working in Mobile Device

  28. 28

    Fontastic not loading on mobile device

  29. 29

    Implement mobile touch keypad in javascript

HotTag

Archive