Make a character jump when user taps the screen of an android device

Hi, I have been trying all sorts of code to make my character jump by tapping the screen. This is for an android device. I have no problems making everything work correctly in Xcode, I just an illogical inability to get things working for android devices.

Thanks in advance

where are you facing troubles ,with recognizing the touches ? or adding the code to manage the physical action of the gameObject (adding some force in Y axis if it is pointing up) ?

1 Answer

1

this will manage each touch (up to five fingers depending on the device).
first off all there are 5 situations a Touchphase will face see here : 1251125

so you have to decide when the action should happen , when beginning touch, or …

here is a small example that will run whatever you want when the touch begins and if the touch not over five fingers or the user stopped touching:

   void Update()
    {
        int fingerCount = 0;
        foreach (Touch touch in Input.touches) {
          if (touch.phase == TouchPhase.Began && touch.phase != TouchPhase.Canceled)
    	 {            
             fingerCount++;
             }
        }           
    }
   if (fingerCount > 0)
      {        
        // manage the Vector or Force Manipulation here of the Player Prefab    
      }

Another Example where you manage if it is only 1 finger touching or 2 and so on:

 using UnityEngine;
 using System.Collections;
    
    public class example : MonoBehaviour {
        void Update() {
            int fingerCount = 0;
            foreach (Touch touch in Input.touches) {
                if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                    fingerCount++;
                
            }
    
            if (fingerCount == 1)
                {
                print("User has " + fingerCount + " finger(s) touching the screen");
            }else if (fingerCount ==2)
          {
             // do another thing if 2 fingers
          }
        }
    }

hope this will help you to start solving your problem.

I have it working now, but am a little confused. Why does if (fingerCount > 0) dir.y = -Input.acceleration.x*.5; transform.position.y += dir.y; } work, but if (fingerCount > 0) rigidbody.AddRelativeForce (Vector3.up * jumpStrength); } does not work? Part of the code is not shown here.

are you getting any error ? i didnt try this method before i think it is better to start a new question, so everyone can find it ,, it is a diffrent matter now ^^

I think you're right.