Simple Correct Jump Movement with Character Controller

Hi all!

I’ve been trying to make my NPCs jump, but calculating a nice movement is a level above my skills :stuck_out_tongue: With the script I got now, the jump seems to get cut off 2/3’s into it, after wich the gameobject falls straight down. Another problem is that the more horizontal speed, the more horizontal the jump direction gets. I have to use a charachter controller, since that is what my AI script is based upon…

So the question is: How do I smooth out the jump itself, and set a correct jump direction, not only controlled by speed?

This is how my script looks like so far:

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

private var direction : Vector3 = Vector3.zero;

function Update() {
var controller : CharacterController = GetComponent(CharacterController);
	if (controller.isGrounded) {
	// We are grounded, so recalculate
	// move direction directly from axes
	direction = Vector3(Input.GetAxis("Horizontal"), 0,
	Input.GetAxis("Vertical"));
	direction = transform.TransformDirection(direction);
	direction *= speed;
		if (Input.GetButton ("Jump")) {
		Invoke("Jump", 0);
 		}
    }
}

function Jump (){
     direction.y = jumpSpeed;
}

function LateUpdate (){
      var controller : CharacterController = GetComponent(CharacterController);
      // Apply gravity
         direction.y -= gravity * Time.deltaTime;
     // Move the controller
     controller.Move(direction * Time.deltaTime);
}

In this test script I’m trying to make a player controlled cube jump. I do the jumping in a seperate function, so I can copy that function to my AI script, where it will be called upon when needed.

Thanks in advance!

Thomas

Well, i’ve done some reading, and decided to go for Rigidbodies after all.

Here’s a good guide about when to use a charachter controller or rigidbody: