I’m making a top down character controller and I’m struggling to get the jumping to work right.
Basically I want the jump to keep the initial movement and slowly slow down, but while it slows it also increases air control.
And it actually works perfect when keeping the initial speed AND slowly increasing air control.
The only problem is that one seems to cancel out the other.
SO if I cut out the section that tells is to keep initial speed, air control gradually kicks in.
AND if I cut out the air control, the initial jump maintains the direction and slows over time.
BUT if I do both at the same time the character launches straight up in the air and slowly gains air control.
IT’S maddening…
Anyway, here’s the function:
void Movement(){
//set inputs
xOffset = Input.GetAxisRaw("Horizontal");
zOffset = Input.GetAxisRaw("Vertical");
//Turn to facing direction
facingDirection = new Vector3(transform.position.x + xOffset, transform.position.y, transform.position.z + zOffset);
transform.LookAt(facingDirection);
//Walking
if (isGrounded == true){
//Apply Movement
movementDirection.Set(xOffset, 0f, zOffset);
movementDirection = movementDirection.normalized * movementSpeed * Time.fixedDeltaTime;
myRigidbody.MovePosition (transform.position + movementDirection);
}
//Jumping
if (jumping == true) {
//Get control values
jumpControl += Time.fixedDeltaTime;
if (jumpControl > jumpControlDelay){
jumpControl = jumpControlDelay;
}
//Add current movement
myRigidbody.MovePosition(transform.position + (movementDirection * (1 - (jumpControl / jumpControlDelay))));
//Add air control movement
airMovementDirection.Set(xOffset, 0f, zOffset);
airMovementDirection = airMovementDirection.normalized * airSpeed * Time.fixedDeltaTime;
myRigidbody.MovePosition(transform.position + airMovementDirection * (jumpControl / jumpControlDelay));
}
Thanks for giving it a look!