- Home /
Rigidbody2d addforce is weaker at a higher y position.
When my player object jumps on a higher terrain, it doesn't jump nearly as high. Here's my jump code.
#pragma strict
var BounceObj : GameObject;
var jumpHeight : float;
function Start () {
}
function Update () {
}
function OnCollisionStay2D (col : Collision2D ) {
if( col.gameObject.name== BounceObj.name && Input.GetKey(KeyCode.W)) {
rigidbody2D.AddForce (Vector2 ( 0, jumpHeight * 10 ));
}
}
So when the cube player goes on the higher ground and I jump, it doesnt jump as high as if it were on the lower floor.
Answer by benandlucy · Jul 14, 2014 at 01:44 AM
Alright I figured it out. I guess it didn't like how I set up the collision if condition
Instead of using a variable to store the floor gameobject, I tried making a tag that every floor object will share, and making the condition use that instead. Here's an example of the code I used.
function OnCollisionStay2D (col : Collision2D ) {
if( col.gameObject.tag== "Floor" && Input.GetKey(KeyCode.W)) {
rigidbody2D.AddForce ( Vector2 ( 0, jumpHeight * 100));
}
}
Hopefully this will help anyone with a similar issue.
Answer by Bunny83 · Jul 13, 2014 at 10:49 PM
Since you use GetKey and OnCollisionStay this might be a problem how long your player touches your collider.
I'm pretty sure that you do multiple AddForce per jump as while holding down your jump key and while you're touching the collider you do an addforce each frame.
If you want a more predictable result, use GetKeyDown instead of GetKey.
This doesn't help, and actually makes the problem worse haha. Also it only does one addforce per jump, as it can only do the command if I press W AND am on the floor. By the time I press W, I would not be on the floor anymore, and the command would not be executed. Also I like how I can hold down W, and jump back up when I hit the floor. Thanks for trying though.
Your answer
Follow this Question
Related Questions
Use addForce to Jump 1 Answer
Character doesn't fall back down when jumping 0 Answers
Jump problem 1 Answer
2d jumping, addForce hitting a max height despite force strength 2 Answers