Make a player jump tile

Hey guys,

What I have is a player that constantly moves forward. When the screen is touched, all of the tiles that are jump tiles respond. The regular tiles do nothing. What happens is that on a jump tile, if the player is on it, it bounces the player upwards to a constant height. It only bounces the player once.

What happens with the script below is that when the bounce () method is activated (external script) The player is shot up like normal, but is really weird, and near the peak of the ascent the player wiggles up and down a bit before finally coming down. Once it hits the ground, it shoots up again - regardless of whether or not the player is on top of a jumping tile. I don’t have to touch the screen. The player just keeps bouncing as if he’s on a giant trampoline.

It’s worth noting that I have my own player physics enabled on the character, which means I’m not using rigidbody gravity. My diagnosis is that in the script, when Rigidbody.Addforce is called, it is called constantly and forever, and my coded gravity overrides it at some point, which allows the character to fall. This would explain the strange phenomenon where near the peak of ascent and descent the player wobbles a bit as if not sure whether to go up or down. This theory would also explain why once the player hits the ground, it shoots back up, because gravitational movement is canceled once the player is grounded.

using UnityEngine;
using System.Collections;

public class JumpBlock : MonoBehaviour {

	private GameObject player;

	public float bounceFactor;
	private bool playerBounceable = false;

	void Awake () {
		player = GameObject.FindWithTag("Player");
	}

	public void bounce () {
		//Play spring animation

		//Bounce character upwards if player is in trigger collider
		if (playerBounceable == true) {
			player.GetComponent<Rigidbody>().AddRelativeForce(Vector3.up * bounceFactor);
			Debug.Log("Bounce!!");
		}
	}

	void OnTriggerEnter (Collider other) {
		if (other.tag == "Player"){
			playerBounceable = true;
		}
	}
	void OnTriggerExit (Collider other) {
		if (other.tag == "Player"){
			playerBounceable = false;
		}
	}
}

There might be 2 possible explanations:

  • You call “bounce” from external script multiple times.

  • The player is moving so fast that OnTriggerExit is not called. This can happen sometimes on fast moving objects. Try to debug your script and see if this is the case. If so, you could check the distance to the ground after a player is bounced and set “playerBounceable” as false if he is in the air.