Search Unity

[SOLVED] speed increase only when button is pressed(touch)

Discussion in 'Scripting' started by Ki4Chan, Aug 12, 2017.

  1. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    I want to increase object's speed only when the button is pressed(touched), it should come back to normal speed when the button is not pressed(touched).

    transform.Translate(direction * Time.deltaTime * speed);

    I tried this;

    public void SpeedUp()
    {
    speed * 10;
    }


    and added this to the button. Speed is increasing when the button is pressed but not coming back to normal when button is left.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
  3. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24


    Code (CSharp):
    1. public bool fingerHold = false;
    2. public Vector2 startPos;
    3. public Vector2 endPos;
    4. public float speed = 1f;
    5. public void Update() {
    6.         PlayerMove ();
    7.     }
    8. void PlayerMove(){
    9.      
    10.         if (Input.touchCount > 0)
    11.         {
    12.             Touch touch = Input.GetTouch(0);
    13.             if (touch.phase == TouchPhase.Began)
    14.             {
    15.                 startPos = touch.position;
    16.                 endPos = touch.position;
    17.                 fingerHold = true;
    18.             }
    19.             else if (touch.phase == TouchPhase.Moved)
    20.             {
    21.                 endPos = touch.position;
    22.             }
    23.             else if (touch.phase == TouchPhase.Ended)
    24.             {
    25.                 fingerHold = false;
    26.             }
    27.         }
    28.         if (fingerHold)
    29.         {
    30.             float deltaX = endPos.x - startPos.x;
    31.             float deltaY = endPos.y - startPos.y;
    32.             Vector2 direction = new Vector2(deltaX, deltaY).normalized;
    33.             transform.Translate(direction * Time.deltaTime * speed);
    34.         }
    35. //This is what I have assigned to the SpeedUp button
    36. public void SpeedUp(){
    37.         speed = speed * 20;
    38.     }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    So, it looks like you have the right idea going. Right now I don't see you calling SpeedUp, so your speed isn't going to increase.

    So, you just need to apply speedUp somewhere, and then if you want a complete return to 1f, you could do that in TouchPhase.Ended. If you want to reduce over time, you could have a speedDown method that also makes sure that you never go below 1f. That will handle it for you.
     
  5. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    That SpeedUp function is assigned to a button UI and it's working when that button is pressed. And for button we don't use touch.phase. It works without doing that.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You could try changing from assignment to the button event to using the IPointerDown/IPointerUp interfaces.
    Set speed up & down on those.
    As @Brathnann mentioned, you could use a slowdown routine if you'd like, also.

    This may require that you check that your touch isn't hitting the button for your other calculations.
     
  7. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That's what I said? :) heh. Glad ya got it working.