- Home /
Fixed it myself.
Confused as to why he only jumps while walking?
And when he runs he gets stuck in the air and can walk in the air? Here's my frankenscript for jumping I've only solved one problem to encounter another and I could really use some help. Not sure why this is so complicated.
Animator anim;
public float jumpSpeed = 8.0F;
private Vector3 moveDirection = Vector3.zero;
private CharacterController _charController;
public float speed = 6.0f;
public float gravity = 10f;
private float verticalVelocity;
private float jumpForce = 5.0f;
private float gravityJump = 14.0f;
void Start()
{
_charController = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
void Update()
{
if (_charController.isGrounded)
{
verticalVelocity = -gravityJump * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space))
{
anim.SetTrigger("Jump");
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
movement = Vector3.ClampMagnitude(movement, speed);
movement.y = gravity;
movement *= Time.deltaTime;
movement = transform.TransformDirection(movement);
_charController.Move(movement);
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity -= gravityJump * Time.deltaTime;
}
Vector3 jumpVector = new Vector3(0, verticalVelocity, 0);
_charController.Move(jumpVector * Time.deltaTime);
}
}
Answer by sh_code · Jan 14, 2019 at 03:59 AM
first of all, this is most likely not a scripting problem, but a problem with your animator state machine. you probably (i'm guessing, since you haven't provided enough info) have transition to jump animation only from walking animation, so when you're idle, ...there's no transition to jump.
also, getting stuck in the air is similar problem, your transition from jump animation back to walking happens mid-jump-animation or something like that.
It plays the jump animation just fine, my problem is the character controller collider stays put on the floor while pressing the jump button unless he's moving forward.
okay, in that case I have no idea.
i recommend rewriting the whole script into something more straightforward, what you're doing is unnecessarily complicated there.
also, gravity (and jump force) should be an acceleration, not speed.
pseudocode:
float verticalAcceleration = 0;
float gravity = -10; // downwards on Y axis is negative numbers
float jumpAcceleration = 30; // upwards on Y axis is positive numbers
void Update(){
if(isGrounded == false){
//we're in the air, so we need to add gravity
verticalAcceleration += gravity;
} else { //we're on the ground, so
//firstly, we don't need to add gravity at all, meaning zero out verticalAcceleration
verticalAcceleration = 0;
//we can do a jump, which modifies our acceleration
if(Input.$$anonymous$$eyDown($$anonymous$$ey.Spacebar){
verticalAcceleration += jumpAcceleration;
}
//now calculate your x and z movement components based on direction keys
float x = whatever; float z = whatever;
}
//NOW, put it all together into a single vector. in a single line. you don't have to mess around with it so many times as you do in your frankenscript
Vector3 final$$anonymous$$ovement = new Vector3(x, verticalAcceleration, z);
//and now just apply that vector to your character
transform.$$anonymous$$ove(final$$anonymous$$ovement);
//and done
}
again, this is just a pseudocode outlining the logic which should actually make sense and not be spaghetticode like your current one. i might have some mistakes there, but it's supposed to show the principle of how to do this in a sane way.
also, note how this will behave - you only add gravity if you're not on the ground, and if you jump, you add 30 to vertical, therefore in the first frame, you'll jump 30 (times deltatime) high, next frame you'll reduce the verticalAcceleration by 10, meaning you'll still go up by 20, next frame you'll go up by 10, next frame you'll stay level, and next frame you'll fall by 10, next frame you'll fall by 20, etc, until back on the ground. this will create at least somewhat natural jump arc. you'll need to tweak the numbers, the gravity as well as the jump, of course, to get the falling speed, jump height, and the arc that you want.
Follow this Question
Related Questions
Jumping on moving platforms problem c# 1 Answer
How can I change jump direction? 1 Answer
Jumping in only the direction of the last key pressed 0 Answers
Jumping in 3D 1 Answer