buttons vs touch.

Hey guys, well basicly I’m making an endless runner for android but I’m having a bit of a problem, when I play on the pc everything is perfect, but once I’ve made a build and test it on the android theres problems. Basiclly it seems that the touch is weaker than the button, my player cant jump, he moves like mm of the floor, but onn the pc its fine. I have jumpForce set to 2, should i keep.changing it and re-building until it woerks.fine.on touch or is there another solution?

using UnityEngine;
using System.Collections;

public class Player_Controller : MonoBehaviour {

	public float jumpForce = 1.0f;
	
	// Update is called once per frame
	void Update () {

		int fingerCount = 0;
		foreach (Touch touch in Input.touches) {
			if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
				fingerCount++;

		}
		if (fingerCount > 0) {
			Debug.Log ("touch"); //debug works
			JumpUp ();
		}
	
		if (Input.GetButton ("Jump")) {  
			JumpUp ();
		}
	}

	void JumpUp () {
	
		GetComponent<Rigidbody> ().AddForce (new Vector3 (0, jumpForce));
	}

}

Interesting.

Assuming this really is all that’s going on, there shouldn’t be any difference that comes from the controls.

I think the issue comes from 2 problems:

  • Your JumpUp is called every frame while the button is held (presumably on purpose)
  • It is called from within the ‘Update’ function

The Update function is tied to the render frame rate of your game, which may well be different on pc to mobile. Additionally it has nothing to do with the actual physics (FixedUpdate).

Right now, when you have jump held:

  • Every Update step, you apply a force to the object to push it up
  • Every FixedUpdate step, the physics engine applies gravity to push it down

As a result, if you are doing 60 fixed updates per second, and on PC you are doing 60 updates per second, but on mobile only 30 updates per second, then your jump will seem twice as weak on mobile.

The correct solution would be to apply your per frame force inside the FixedUpdate function:

public class Player_Controller : MonoBehaviour {
 
     public float jumpForce = 1.0f;
	 bool thrusting = false;
     
     // Update is called once per frame, and is linked to rendering and input events
     void Update () {

		 //clear our thrusting boolean
		 thrusting = false;
 
		 //check fingers
         int fingerCount = 0;
         foreach (Touch touch in Input.touches) {
             if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                 fingerCount++;
 
         }

		 //mark as thrusting if pressing a finger or holding the Jump button
         if (fingerCount > 0) {
			 thrusting = true;
         }
         if (Input.GetButton ("Jump")) {  
			 thrusting = true;
         }
     }

	 // FixedUpdate is called once per physics step, and we should do any physics dependent operations here
	 void FixedUpdate() {
		 //if we should be thrusting, apply an upwards force
		 if(thrusting)
			GetComponent<Rigidbody> ().AddForce (new Vector3 (0, jumpForce));
	 }

 }

it’d be well worth reading up on the difference between Update and FixedUpdate - do a google and there’s lots of info on them. They’re quite important concepts especially when you start doing physics stuff like your jumping.

-Chris