Been trying to make a jump that will almost “hang” in the air. Where the jump will slow down as it reaches the apex of the arch before gravity pulls it down as it regularly does.
Currently I’m using this…
rigidbody2D.AddForce(new Vector2(0, 10)
But it will just add a set force which will be decreased at the same rate by the gravity value. It won’t give me the “hang” I’m looking for.
Is there a way to make that “AddForce” value exponential where as the vertical force gets closer to 0 the rate at which it decreases will slow down? Any pointers would be appreciated!
Using forces in this situation is something you shouldn’t do.
The general way of doing basic jumping in any 3D or 2D game using a rigid body to control such character, is to directly modify the y axis of the velocity of the rigid body when its grounded, like so:
Vector3 jumpVelocity;
jumpVelocity.x = rigidbody.velocity.x; // Preserve the x and z axis of the velocity
jumpVelocity.z = rigidbody.velocity.z;
jumpVelocity.y = 10.0f; // The jump force
rigidbody.velocity = jumpVelocity;
You can still use Rigidbody.AddForce, but usually not, and if you would, it would most likely have a ForceMode.Impulse modifier apart of it, like so:
rigidbody.AddForce(new Vector3(0, 10, 0), ForceMode.Impulse);
Now, you want to have a hanging “cartoonish” effect when your character jumps. Using either of these options is plausible, but would require a large amount of fine tuning to get your character to do what you please.
The final thought is that what you can do is 0 out the y velocity for the rigid body for a set amount of time, but then “let go” of the rigid body by letting the y axis of the velocity be free. I wish I could give you an example, but I don’t have the time right now.
Hope this helps.
if you need something special its simple enough to turn of the characters gravity in the inpectors rigidbody component and just script it yourself. just got to be creative thats all.
simple up and down code might be
var myweight:float=.01;
var legstrength:float=1;
var myweightafterjump:float=.005;
var spot:float;
var groundlevel:float;
var jumping:boolean; //----------make this true to jump!!
groundlevel=transform.postion.y;
function Update (){
if(jumping==false){spot=legstrength;}
if(jumping==true){
if (spot>0){spot=spot-myweight;}else{spot=spot-myweightafterjump;}
transform.position.y=transform.position.y+spot;
}
if(transform.position.y<groundlevel){jumping=false;transform.position.y=groundlevel;}
i didnt test this so you got to debug and play with the numbers at the top. but to get you started. basically slowly taking away from a number that is added to the y position creates a smooth arch instead of a jagged down movement if you directly assign y. i put another variable in there so you can make the down movement a little slower so it looks like he hangs a bit.
you pry want to replace the “ground level” part with an OnCollision Detection.
if you want to get more control,
multiplying things with “distance to start”. can also make a jump soothly go faster as it goes higher like a rocket or slower as it reaches ground like a hovercraft or really whatever you want something to do can be done with a simple math. Unfortunatly, they dont tell you all stuff this in the tutorials!
Thanks for all the help guys. I actually thought of another way to handle it was well, when the vertical force decreases to a certain threshold but before it hits 0 I decrease the drag on the rigidbody so you are given more “airtime”.
I’ll give the alternatives everyone presented a look as well. Thanks!