x


How to jump with the Character Controller

Hello everyone, I'm trying to make a Character Controller to jump, but it doesn't works properly. This is my script:

var speed : float = 3.0;
var gravity : float = 20.0;
var rotateSpeed : float = 3.0;
var jumpSpeed : float = 8.0;
private var moveDirection : Vector3 = Vector3.zero;

function Update () {
    var controller : CharacterController = GetComponent(CharacterController);
if(controller.isGrounded){   
transform.Rotate(0,0,Input.GetAxis ("Horizontal") * rotateSpeed);

transform.Rotate(0,0,Input.GetAxis ("Unhorizontal") * -rotateSpeed);



var forward : Vector3 = transform.TransformDirection(Vector3.left);
var curSpeed : float = speed * Input.GetAxis ("Vertical");

 if (Input.GetButton ("Jump")) 
 {transform.Translate(Vector3.forward * jumpSpeed * Time.deltaTime);}

}

moveDirection.y -= gravity * Time.deltaTime;
controller.SimpleMove(forward * curSpeed);

}

@script RequireComponent(CharacterController)

Can you explain me the proper way to do it please?

more ▼

asked Jan 20, 2011 at 02:29 PM

Hektor gravatar image

Hektor
35 25 18 26

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You forgot 'controller.Move(moveDirection * Time.deltaTime);' to the bottom where you have the simple.move line.

try this

var speed : float = 3.0;
var gravity : float = 20.0;
var rotateSpeed : float = 3.0;
var jumpSpeed : float = 8.0;
private var moveDirection : Vector3 = Vector3.zero;

function Update () {
var controller : CharacterController = GetComponent(CharacterController);
var forward : Vector3 = transform.TransformDirection(Vector3.left);
var curSpeed : float = speed * Input.GetAxis ("Vertical");transform.Rotate(0,0,Input.GetAxis ("Horizontal") * rotateSpeed);

transform.Rotate(0,0,Input.GetAxis ("Unhorizontal") * -rotateSpeed);

controller.SimpleMove(forward * curSpeed);

if (Input.GetButton ("Jump")) 
if(controller.isGrounded){ 
moveDirection.y= jumpSpeed;

}

moveDirection.y -= gravity * Time.deltaTime;

controller.Move(moveDirection * Time.deltaTime);
}
more ▼

answered Jan 20, 2011 at 08:41 PM

superventure gravatar image

superventure
760 153 130 153

Thank's for your time! Now it works at all!

Jan 21, 2011 at 01:14 PM Hektor

the code you given is not smooth only the falling is(its by charachter controller)this code just teleports the player to a higher position and let the character controller do the rest, any idea how to make it smooth?

Aug 16, 2012 at 12:38 AM robotmaker01

Nice code .It helped me...

Nov 29, 2012 at 11:56 AM sona.viswam

You don't need to call the variable of controller every frame. Put it outside of update and assign it in the inspector for better performance.

Dec 17, 2012 at 02:01 AM Coreyf716

This provided a great deal of help, thank you!

May 23, 2013 at 09:14 AM Hala
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1122
x672

asked: Jan 20, 2011 at 02:29 PM

Seen: 12983 times

Last Updated: May 23, 2013 at 09:14 AM