- Home /
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;
}
}
}
Answer by Tehnique · Jul 16, 2014 at 07:44 PM
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.
I know that I'm not calling it multiple times because the debug log "Bounce!!' would keep appearing, but the other two ideas are good! Since my player would move at multiple y values, do you know how I would use raycasts to detect how far from the ground I am?
Well, for the moment you can check if that is the case by logging "trigger enter!" and "trigger exit!". Check if there is an exit called when you bounce.
The simplest way to do detect height it is without raycasts. Just save the Y of the player when the bounce starts. In update, if playerBounceable is true check the difference between player Y and initial Y. When the difference is >x (a constant you set, that is the height to stop applying force at), set playerBounceable to false.
I feel so stupid for not thinking of that myself. As it turns out actually I actually had a boolean in the player physics script that checked if the player was on the ground (as you can tell I do not have a good memory) so that's ok. I'm rewriting the other parts of the jump code to make the jump dependant on the player physics and not the rigid body, because I never liked rigid bodies anyways. I'll let you know how it is in a sec