Rigidbody.AddForce does not add force to the value I set it as!
So I’m trying to script a sidescroller platformer character without using the character controller. When I attempted to use the character controller everything got weird and the character would fly through solid objects. Yeah, um, no thanks.
Everything works fine with my own script except the jumping. I have yet to find anything in Unity as incredibly difficult as programming basic jumping.
I have been using “Rigidbody.AddForce” to jump, and it kind of works except every so many jumps it randomly causes my character to fly up very fast and high. For gravity I’m using the gravity from the rigid body.
Here is my code
private var grounded : boolean = false;
public var jumpVelocity : Vector3;
private var jumping : boolean = false;
function Start () {
jumpVelocity = Vector3(0, 5, 0);
}
function Update () {
//Jump
if(grounded (Input.GetButtonDown("Jump") || Input.GetButton("Jump")) jumping == false){ //grounded checks out correct
//if(rigidbody.velocity.y == 0){rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);} //<Tried this
//rigidbody.AddForce(jumpVelocity, ForceMode.Impulse); //<Tried this
rigidbody.AddForce (0, 100, 0);
jumping= true;
}
//Rest of script....
Is there any alternative to “Rigidbody.AddForce” that doesn’t require me to use the character controller? I would like my character to jump to the same height every time I jump, not random heights each time.
I tried it, but it didn’t work. The character still jumps really high randomly every few jumps.
I set the vector to (0,1,0) and I print the vector, it shows the rigidbody.velocity on the Y axis to be between around 2 and 3 and sometimes jumps to 6 or so. Come on Unity!
EDIT
Solution:
I tried
rigidbody.velocity = Vector3(0,8,0);
And while it still fluctuates a little, it does not appear to have the problem of sometimes blasting my character off into oblivion when jumping. I’m tired and don’t have time to thoroughly test it, but I think you were onto the right idea. Thanks.
One more thing you could try in case the other solutions don’t solve your issue. Use ForceMode.Impulse as the second argument to the AddForce method. After all, if you’re trying to mimicking a jump, a single force is more realistic than a continuous one, and should give more consistent results.
Yeah I couldn’t remember if I had set it directly as you ended up doing. The only thing that might be a problem with that code is it might stop any left or right movement, which is generally important for a platformer, but thats ok, you just add it into the velocity.
Also, definitely put anything rigidbody related into fixedupdate.
heh, just decided to go confirm you could read key presses from fixedupdate, and it seems setting the velocity is the Unity standard way for doing a jump…
I know this is solved but I would just like to add, you can remove || Input.GetButton(“Jump”)) jumping == false from your if statement and the jumping bool all together.
I’m pretty sure Input.GetButtonDown() only returns the button once every press.