Natural Jump algorithm

Hi,

I’m struggling to get my jump non linear. Right now, the character just rises and then falls, in very linear fashion. Using “Move”, from character controller on other part of the code. The relevant part of the code is this:

function nJump() {
	if (nJumping) {
	
		nNewPos.y += jumpSpeed * Time.deltaTime;	
					
		if (!(nControl.collisionFlags & CollisionFlags.Above) && transform.position.y  < nOldPos.y + maxJumpHeight) {	
			nCanFall = false;							
		}
		else if (nControl.collisionFlags & CollisionFlags.Above || transform.position.y > nOldPos.y + maxJumpHeight) {
			nJumpEnd = true;
			nJumping = false;
			nCanFall = true;
		}	
	} 
	
	if (nControl.isGrounded && nJumpEnd) {
		nGroundAfterJump = true;
		nJumpEnd = false;
	}
}

function nFall () {
	if (!nControl.isGrounded && !nJumping && nCanFall && !nCanClimb) { 
		nNewPos.y = -gravity * Time.deltaTime; 
		nFalling = true;
	}
	else if (nControl.isGrounded && !nCanClimb)	{
		nFalling = false;
		nCanFall = true;
	}	
}

What’s making everything linear is probably this:

nNewPos.y += jumpSpeed * Time.deltaTime;

and this:

nNewPos.y = -gravity * Time.deltaTime;

No matter what I try, I just get some weird accelerations and non desired behaviors. Can you help me there?

Ok the way you handle your movement you can’t really simulate accelerated movement. For this you need a speed variable that is changing over time.

So the usual procedure is to set or increase a speed variable to make the character move. To jump you just add a one-time vertical speed-boost. Gravity will bring your character down again.

private var velocity : Vector3;
var speed = 10.0;
var jumpSpeed = 12.0;
var gravity = Vector3(0,-9.81 ,0);

function Update()
{
    if (nControl.isGrounded)
    {
        velocity = Vector3(Input.GetAxis("Horizontal") * speed,0,0);
        if (Input.GetButtonDown("Jump"))
        {
            velocity.y += jumpSpeed;
        }
    }
}

function FixedUpdate()
{
    if (!nControl.isGrounded)
    {
        velocity += gravity * Time.deltaTime
    }
    nControl.Move(velocity * Time.deltaTime);
}

Accelerated movement should be done in FixedUpdate which runs at a constant framerate.

Basically it’s like the basic example from the CharacterController reference page

Thank you. I’ve researched some projectile motion stuff, and the “character move” sample from Unity Reference. Made some progress, but couldn’t get my functions to work properly yet.

Right now, I got this for gravity:
nNewPos.y -= gravity * Time.deltaTime * Time.timeScale;

And this for jump:
nNewPos.y += jumpSpeed * Time.deltaTime * Time.timeScale;

They kind of work smoothly, but the jump accelerates a in the middle of the motion (instead of the begining)… and both animations don’t respect Time.timescale (therefore, it can’t be slowed down, or keep the same speed on different computers). I’ve tryied with the * Time.timescale, and without it.

Would you point me a direction to fix this, and still keep the smooth jump/grav effect?