make an object jump

Hi All

I am stuck with a problem i need to make a object “jump” to a perticluar height, right now my object jumps on clicking a key but if I click the key while the object is in air it jumps again, but if i want to make it jump only once what should i do. Also I want to restrict the jump hight of the object.

Add a variable when you jump, jumping = true; then when you land, jumping = false.

Max height of jump can be attained through gravity, or if you use a timer to estimate when the player will peak.

Hey thanks

but how can i make it jump only once after pressing the key?

currently I am using the following cade to make the object jump

if(Input.Getkey(“space”)){
object.rigidboady.AddForce(Vector3.up * jumphight);
}

NOTE: I am setting the jumphight externially so as to controle it :slight_smile:

if(Input.Getkey("space")  jumping == false){ 
object.rigidboady.AddForce(Vector3.up * jumphight); 
jumping = true;
}

Then when they land, jumping = false;

1 Like

Hey thanks il check it out :smile:

Hey DarkReaping

I tried using the method that u told but then my object has stoped jumping don’t no why?

Iam taking the “Jumping” as a “bool” datatype n initializing it to “flase” in the begning then in the function the controles my jump I have placed the code as explained, but now it has stoped jumping comeletely. Also im calling the junction that controles my jump in the “Update” finctions.

Is jumping always equal to true?

No I have set “jumping” to False as default.

I have taken “Jumping” as a private bool datatype

" private bool Jumping = false; "

deployed the following code in my jump_controle()

if(Input.Getkey(“space”) jumping == false){
object.rigidboady.AddForce(Vector3.up * jumphight);
jumping = true;
}

what could i possible be doing wrong

NOTE: Iam calling the “jump_controle()” function through update()

i use this it is a bastardised version of the fpswalker script (i use this for a 3rd person object) that sets double jump to false once used and true once (grounded) you could remove the a/d key controll there is also a checkbox that allows to enable and disable doublejump in the editors inspector pane it requires the character controller component (Comes as part of unity) but it should work for you.

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var DoubleJump = true;
var HorizontalMovement = true;
private var DJ : boolean = true;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

function Update() {
	if (grounded) 
	{
		
		DJ = true;
		
		if (Input.GetButton ("Jump")) 
		{
			moveDirection.y = jumpSpeed;
	
		}
	}
	else
	{
		if (DoubleJump == true)
		{
			if (DJ)
			{
				if (Input.GetButtonDown ("Jump")) 
				{
					moveDirection.y = jumpSpeed;
					DJ = false;
				
				}
			}
		}
	}
	if (HorizontalMovement == true)
	{
		if (Input.GetKey ("d")) 
		{
			moveDirection.x = speed;
		}
			else
			{
				if (Input.GetKey ("a")) 
				{
					moveDirection.x = -speed;
				}
					else
					{
						
						moveDirection.x = 0;
					
					}
			}
			
	}
	
	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)

hope this helps you should only need to modify it a little bit

hey thanks il try it out :smile:

var jumpSpeed = 8.0; 
var gravity = 20.0; 
var DoubleJump = true; 
private var DJ : boolean = true; 
private var moveDirection = Vector3.zero; 
private var grounded : boolean = false; 

function Update() { 
   if (grounded) 
   { 
       
      DJ = true; 
       
      if (Input.GetButton ("Jump")) 
      { 
         moveDirection.y = jumpSpeed; 
    
      } 
   } 
   else 
   { 
      if (DoubleJump == true) 
      { 
         if (DJ) 
         { 
            if (Input.GetButtonDown ("Jump")) 
            { 
               moveDirection.y = jumpSpeed; 
               DJ = false; 
             
            } 
         } 
      } 
   } 
             
   // Apply gravity 
   moveDirection.y -= gravity * Time.deltaTime; 
    
   // Move the controller 
   var controller : CharacterController = GetComponent(CharacterController); 
   var flags = controller.Move(moveDirection * Time.deltaTime); 
   grounded = (flags  CollisionFlags.CollidedBelow) != 0; 
} 

@script RequireComponent(CharacterController)

I had am moment and just removed the a/d key controll components and just reduced it down to a basic jumping script that allows for double jump to be selected from the inspector :twisted:

Hey,

This script is pretty cool but I ran into a little issue when trying it out. When the play jumps they go out and fall down like expected, but if the Player runs off an edge they dead drop super fast. It seems pretty jarring and unnatural.

-Mike

I know this is a scaling issue i found that if you scale down the scene and reduce the gravity and jump values you get a better response but sometimes scaling down causes some errors like the ball jumping upwards in a diagonal direction when you move. i’m looking into a method of fixing these issues so if anybody has any ideas on how to fix this let me know and i’ll try them out :smile: see if we can make this puppy work the way it should.

funily enough i have no problem with the character bouncing down slopes with this script. though i recon i might if i do fix the other issues.

Ok so i messed about with it and cleaned up the code and the sudden falling doesnt happen anymore it appeared that the effect of gravity was building up when grounded so i only apply gravit when not grounded now and it works fine the new “working” code is as follows

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var DoubleJump = true;
var HorizontalMovement = true;
private var DJ : boolean = true;
private var moveDirection = Vector3.zero;
static var grounded : boolean = false;

@script AddComponentMenu("Playa/Player Control")

function Update() 
{
	if (grounded) 
	{
		
		DJ = true;
		
			if (Input.GetButton ("Jump")) 
			{
				moveDirection.y = jumpSpeed;
			}
	}
		else
		{
			if (DoubleJump  DJ  Input.GetButtonDown ("Jump"))
			{
				moveDirection.y = jumpSpeed;
				DJ = false;
			}
		}
	if (HorizontalMovement  Input.GetKey ("d"))
	{
		moveDirection.x = speed;
	}
		else
		{
			if (Input.GetKey ("a")) 
			{
				moveDirection.x = -speed;
			}
				else
				{
					moveDirection.x = 0;
				}
		}
			
	
	
	// Apply gravity
	if (!grounded)
	{
		moveDirection.y -= gravity * Time.deltaTime;
	}
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)

hope this is acceptable please post back or PM me with any feedback or issues with the code and i’ll get right back on it.

Sweet, I hate when the fix is something little and simple. :smile:

I’ve been trying to fix this into iPhone but I hit a snag with the GetButton vs GetButtonDown, Tapping a guiButton doesn’t have the two different calls, so the double jump doesn’t work. But other than that, great code.

-Mike

You can make both GetButtonDown actually that’s just a minor mistake on my part that works because of the grounded state or make them OnGUIButton or OnGUIButtonDown i think. simply change the button press to a gui button basicly :twisted:

Hi, I have a problem with my jumping script. It is simply :

#pragma strict
Var jumpH = 8;
Var isfalling = false;
Function Update ()
}
If (Input.GetKey(KeyCode.C)&& isfalling = false)
}
Rigidbody.velocity. Y = jumpH;
isfalling = true;
{

}

Function OnCollisionStay()
}
isfalling = false;

{

The jump works fine but if I bump into a wall I can jump up it and I want it to not jump up the wall. If anyone could help me with this that would be cool.

I need to add a jumping action to mine, im working on a third person shooter…I can move forward and go in circles using mouse, but need to add, left. Right. Back and jump…used for scripts agentcontroller and agentinput… this is for a mobile game.