Search Unity

Jump With Touch

Discussion in 'Scripting' started by imunirbesye, Feb 9, 2019.

  1. imunirbesye

    imunirbesye

    Joined:
    Oct 27, 2018
    Posts:
    57
    I did what I wanted with "space" button but when I turn it to touch it goes further.

    Code (CSharp):
    1. if (Input.touchCount == 1)
    2.         {
    3.             gameObject.GetComponent<Rigidbody>().AddForce(new Vector3(0, 1, 0) * jumpSpeed, ForceMode.Impulse);
    4.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    If that is in your Update function then every single frame that you have one touch, it will add more impulse.

    If you store the previous frame's touch count and compare it with this frame's touchcount, then you can instead only do the jump when you have one touch this frame, and zero touches last frame.
     
  3. imunirbesye

    imunirbesye

    Joined:
    Oct 27, 2018
    Posts:
    57
    I did it with TouchPhase thank you too.
     
    Kurt-Dekker likes this.