How do I fix my player jumping code?

Help everyone, I have the following variable in PlayerMovement.js, the script that I use for movement:

var jumpSpeed = 1.0;

I also have the following Update() function:

function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);

// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);

// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);

// Jump code
	if (Input.GetButtonDown("Jump"))
	{
	var upward = transform.TransformDirection(Vector3.up);
	controller.Move(upward * jumpSpeed);
	}

// Animate character
if (Input.GetAxis("Vertical") > 0.1 || Input.GetAxis("Horizontal") > 0.1 || Input.GetAxis("Vertical") < -0.1 || Input.GetAxis("Horizontal") < -0.1)
       animation.CrossFade("run");
       else
       {
       animation.CrossFade("idle");
       }
}

The character’s movements and animations work just fine, but currently, the jump code merely drops the player character from a height equal to jumpSpeed above the player’s position in 3d space. The character must obviously have a smooth vertical transformation before dropping. Please help, it will be appreciated.

MachCUBED

SimpleMove takes care of the vertical movement to include gravity, what is causing this weird behavior. You should use only Move to be able to jump, and apply gravity yourself.

Unlike SimpleMove, to which you pass the velocity, Move requires a displacement. Fortunately, you just need to multiply the velocity by Time.deltaTime to convert it to a displacement:

var jumpSpeed: float = 5.0;
var speed: float = 6.0;
var rotateSpeed: float = 60.0;
var gravity: float = 10.0; // gravity acceleration

private var vSpeed: float = 0; // store vertical speed in a separate variable

function Update ()
{
    var controller : CharacterController = GetComponent(CharacterController);
    // Rotate around y - axis
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed * Time.deltaTime, 0);
    // Move forward / backward
    var curSpeed = speed * Input.GetAxis ("Vertical");
    var moveDir = transform.forward * curSpeed;
    // Jump code
    if (controller.isGrounded){ // if character is grounded...
        vSpeed = 0; // its vert speed is zero
        if (Input.GetButtonDown("Jump"))
        { // but if jump pressed, set it to jumpSpeed
            vSpeed = jumpSpeed;
        }
    }
    // apply gravity acceleration
    vSpeed -= gravity * Time.deltaTime;
    moveDir.y = vSpeed; // include vSpeed in  moveDir
    // moveDir * Time.deltaTime is the displacement since last frame
    controller.Move(moveDir * Time.deltaTime);
}

NOTE: remember to set jumpSpeed to a more suitable value - 5 to 10 - at the Inspector; the Editor has an elephant memory, and will keep the old value (1.0) to death, which will make the jump a mere hiccup instead of a decent jump!

Hello,

Is your character a Rigidbody ?
If it is, you should try to change the velocity variable on it, making it somthing like a Vector3(0, jumpSpeed, 0);

Hoping I helped you