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.
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.
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
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:
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.
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 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.
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.
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.