Need help with Multi Jump script

I am making a 2d sidescroller where u jump multiple times to get to another side of lava pits and such.

The problem I have is that when the character jumps it teleports up and doesn’t smoothly increase in altitude. I think this is due to the transform.Translate part of the code but I just don’t how to to imply the AddForce or the velocity.

Another problem I have is that the gravity increases over time which makes it fall faster and faster which I do not want. I want it so every time you jump the gravity resets so it looks like a natural multi jump.

I am using this script with the Character Controller, Character Motor, and the FPSInputController

Heres the code :

function Start () {
}
 
var moveSpeed : float = 0.15;
var jumpSpeed : float = 4;

function Update(){

   if (Input.GetKey("d"))
     transform.Translate(-moveSpeed,0,0);
 
   if (Input.GetKey("a"))
      transform.Translate(moveSpeed,0,0);
      
   if (Input.GetKeyDown("space"))
      transform.Translate(0,jumpSpeed,0);

}

Please I really need help.

What you want will probably require storing a vertical velocity variable.

Your problem is more on the physics side than the scripting side. Gravity should remain constant, but that means a constant acceleration, which is a constant change in velocity. And so then when you jump you would set the velocity to jumpSpeed and when you land, set it to 0.

Platformers can be trickier than you might expect. You might do better to practice on something more akin to Asteroids or Galaga until you’re comfortable with the physics of it. Or like Flappy Bird, because the velocity and gravity is there but you don’t need to handle your bird landing. Or you could retool the game to rely upon the rigidbody physics (which has its own velocity built in) and then all your controls will use .AddForce() instead of .Translate()

Flappy Birds is exactly what I am aiming for actually. I just can’t get it to have a smooth increase in altitude. I have tried using rigidbody.Addforce with the forcemode.impulse. The main problem I have right now is that using the physics like rigidbodys nothing happens. Sometimes it makes the camera go up and then start jumping around on the way coming back down but the player stays stationary. I can post a video or my other codes if needed.

Check this video (not mine) for a player controller script:

Try that, post the code and I will show you how to add double jump to it.

Are these necessary?

Character Controller, Character Motor, and the FPSInputController

Thank you very much. I just took away the three that you asked if they were necessary and put on some of the code that guy mentioned and put some of my own for controlling it left and right. The only thing now is that the player doesn’t always respond to the jump. Every now and then it will not pick up the spacebar being clicked and sometimes it responds perfectly.

function Start () {
}
 
var moveSpeed : float = 0.05;
var jumpSpeed : float = 4;

function Update(){

   if (Input.GetKey("d"))
     transform.Translate(moveSpeed,0,0);
 
   if (Input.GetKey("a"))
      transform.Translate(-moveSpeed,0,0);
      
}     
function FixedUpdate(){
	if (Input.GetKeyDown("space"))
		rigidbody.velocity.y = jumpSpeed;
}

There is only one other script on the player which is :

#pragma strict
var speed : float = 4;

function Start () {
}

function FixedUpdate () {
	rigidbody.velocity.x = speed;
}

dont need this so remove it from the player

    #pragma strict
    var speed : float = 4;
     
    function Start () {
    }
     
    function FixedUpdate () {
        rigidbody.velocity.x = speed;
    }

Also do the exact code that geezer does in his video as it is a better way of moving left and right.

Well I am using that to permanently move the player to the right so its automoving. Also I did change the way to control the player.

OK, but its not a good idea to have 1 script moving the object by using the keys and another to keep the player moving.

If you want the player to constantly move in the x direction using the script with
function FixedUpdate ()
{
rigidbody.velocity.x = speed;
}

Then why have you got another script to move the player with the d and a keys.

Also why not put rigidbody.velocity.x = speed; in the same FixedUpdate () in the other script.

GetKeyDown should be in Update not FixedUpdate that’s why it doesn’t always work.

http://forum.unity3d.com/threads/56862-FixedUpdate-and-Input-GetKeyDown

I got the script from the video working with a double jump.

#pragma strict
var speed : float =4;
var jumpHeight : float =20;
var jumpCount : int=0;

function Update()
{
	if(IsGrounded()  jumpCount !=0)
	{
		jumpCount=0;
	}
	// Handle horizontal movement
	rigidbody.velocity.x = speed*Input.GetAxis("Horizontal");
	
	// Jumping max of 2 jumps (double jump)
	if(Input.GetKeyDown(KeyCode.Space)  jumpCount<2)
	{
		rigidbody.velocity.y = jumpHeight;
		jumpCount++;
	}
}

function IsGrounded()
{
	// Check if on ground with a raycast
	return Physics.Raycast(transform.position, Vector3.down, collider.bounds.extents.y+0.01);
}

Thank you very much for both of your responses. Softwizz I was going to change that I just hadn’t got to it yet and wasn’t really thinking things through. Also thank you for the script I will add that into my project. Also rrh, for future knowledge what should i use instead of GetKeyDown or should I put it in Update but I have been told not to use rigidbodys in the Update().

Put GetKeyDown in Update, not FixedUpdate.

The warning against Update applies if you want to do something constantly, rather than just once. I would actually be more suspicious of your first example where you did this in Update:

if (Input.GetKey("d"))

     transform.Translate(-moveSpeed,0,0);

Because that’s supposed to be constant movement.