So, my character doesn’t jump properly. I have tried a few things and the current state is that if I press space the character nudges up a small amount and back down. Trying to get the character to jump as you would expect from a 2d side scroller and I know I am just overlooking something, so any guidance is appreciated.
I was going to fix that issue with double jumping later. Right now I am just trying to get controlled jumping working cause I whipped out what I had before to try and simplify lol. Right now it nudges up maybe .2 and then back down.
What I am mostly trying to work on is getting the character to jump up gracefully. The “gravity” seems to be working okay now. The character seems to make the upwards part of the jump almost instantly and I want to smooth it out. Gravity is set to 4 and jumpspeed is 80. The 80 seems to give me the height I need.
So, still trying to refine my jump. Applying “gravity” makes the character fall at a rate I like. My problem now is trying to get the character to move upwards at the same rate.
well, If I would make a character jump I would use just and only forces like
rigidbody.addforce(Vector3.up, jumpspeed);
rigidbody.addforce(trasform.forward,forwardspeed);
than apply gravity and drag in order to make it falling back and stopping.
I thought I had read somewhere that rigidbody movement didn’t work well with character controller attached. Is this not the case.
I did try to add force before but have never got the character to move. Tried just now and still the same, even with the gravity line of code commented out. Just not sure what I am overlooking.
This way you instantly accelerate character the instance space is pressed and then allow Unity physics engine to handle the rest - apply gravity to decelerate your jumpVel and then accelerate downwards.
EDIT:
We now use AddForce instead of Move anyway so no need using CharacterController
If you want your character no to be affected by other physical happenings then you can make RigidBody Cinematic. AddForce will longer work I think and you’ll have to apply gravity, movement etc. manually like you did with CharacterController. You say you had downward movement working. Upward is the same just opposite direction.
Collisions will work perfectly with a rigidbody. You will also get perfect momentum, falling, bouncing, friction, etc. and everything else the physics engine can do. The problem with using a rigidbody is that you have to constrain that motion into something your player can control easily. This can be very difficult because you have to fight against the physics engine by applying forces, constraining axes, tweaking materials, etc.
The character controller is entirely the opposite approach. A character controller gives the player precise and responsive controls, but you have to fake enough physics so that the movement feels fluid. This is what people generally use because it is a bit easier unless you really need to have authentic physics for your players.
This is also why you can’t combine rigidbodies and character controller. The rigidbody is giving all the control to the physics engine while the character controller is trying to control the movement so they just end up fighting and nothing works.
The problem in your case is that you are not conserving momentum. You jump by simply moving the character in a single frame so that is exactly what you get. Your problem is that you are zeroing your MoveVector every frame:
MoveVector = Vector3.zero;
What you need to do is use a velocity vector instead.
void checkMovement()
{
// Friction is a factor between 0 and 1 that reduces your characters speed.
// Low values gives lows of control, and higher values makes for a slippier surface
// Only applied only to horizontal movement
// You will also want to change it if the character is in the air or on different surfaces
VelocityVector.x *= friction;
if(Input.GetKey(KeyCode.D)){
VelocityVector += MoveSpeed * Time.deltaTime;
faceDirection = 1;
// Note, with this code a player could be facing one direction
// while still moving in the other if they had a lot of speed
// and are on a slippery surface. That may or may not be what you want.
}
else if(Input.GetKey(KeyCode.A)){
VelocityVector -= MoveSpeed * Time.deltaTime;
faceDirection = -1;
}
if(Input.GetKeyDown(KeyCode.Space)){
if (playerIsGrounded || playerCanDoubleJump){
VelocityVector += JumpSpeed * Time.deltaTime;
}
}
VelocityVector -= Gravity* Time.deltaTime;
CharacterController.Move(VelocityVector);
}
Since you are not zeroing out the velocity vector, the JumpSpeed that you applied last frame will be conserved. Each frame will add more gravity until your entire jump velocity is gone and your character will start falling.
It has been a while since this thread has a post, but I recently came up against this very issue, and the solution is to use a coroutine to create a smooth set of sequential upwards motions.