- Home /
Basic Jump Pad help
Hey everyone, I need some help with creating a basic jump pad. My idea is that when the player collides with a specific gameObject (the jump pad), it will add a force to the player on its Y-axis, making the player "jump". I've tried using rigidbody.AddForce, but it does not seem to work. If i untick "Is Trigger" on both the player and the jump pad, the collision register, but no force is added.
Any help appreciated!
Hi, some more details would be nice.
Show your code, what exactly did happen with rigidbody.AddForce?
JS:
var speed:int = 5;
function OnControllerColliderHit(hit:ControllerColliderHit) {
if(hit.gameObject.tag== "Player") { Debug.Log("Ok"); hit.rigidbody.AddForce(Vector3.up * speed); } }
On collision, nothing happens to the player. No force is added no matter how high i set the value. I'm thinking it has to do with the type of collision i used, not sure though.
ok,
what exactly did happen while using this code?
do you have any console output?
Since I dont know you project I cant say anything perticular but you could check if hit.rigidbody is returning null.
Answer by Jwizard93 · Jul 09, 2017 at 10:27 PM
So I don't use unityScript and I didn't use a player controller. Just a standard collision between two box colliders. I didn't see anything happening until I raised the value of "speed" 500 was good for me. Heres the code for jumpPad:
public class jumpPad : MonoBehaviour {
public int speed;
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Player"))
{
other.gameObject.GetComponent<Rigidbody>
().AddForce(Vector3.up*speed);
}
}
}
replace the () with the name of your rigidbody
Answer by AntiBeta · Feb 07 at 10:58 PM
If your jumping script also use AddForce() the jump pad script and the jump script can be overlapped thus: you need to reset your characters RBs velocity before jump pads code runs. example is under below
other.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
other.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up*bounce, ForceMode2D.Impulse);
also; if u want to save your horizontal velocity you can use like this:
other.gameObject.GetComponent<Rigidbody>().velocity =
new Vector3(other.gameObject.GetComponent<Rigidbody>().velocity.x,0,0);
Do not forget if your game is 2D you need to change Rigidbodys => Rigidbody2D and Vector3 => Vector2
good luck :)