- Home /
Is there any way to do a double jump with the first person controller?
Hey, I'm working on a platformer game and I'm re-working the first person controller for the player character. Everything has been fine so far, but I need to make the character be able to jump in midair. I think the changes I need to make are somewhere in the character motor script, but I'm not 100% certain. Any help is very much appreciated.
I also found a Unity Package that some guy made back in 2010 that was a modified version of the first person controller to make it function more like the Unreal Tournament 3 player character, which would allow for double jumping. Unfortunately, all of the scripts were missing when I imported it, so I'm not sure if I'm doing something wrong, or if the package is corrupted. If anyone knows a solution to that, I'd also appreciate it.
Please post your script or we cant really help you much.
@superluigi - I believe he is using the standard first person controller script.
There is unfortunately I just updated unity and npw monodevelop isn't working lol when I get it working I'll try to help u out. $$anonymous$$eep in $$anonymous$$d I'm very new to this so I might not be able to help much but I do have a successful double jump script on my character controller albeit its a 2d game.
Yeah, I'm using the standard first person controller prefab from the character controller package. I'm just having trouble finding which script handles jumping and where to modify code.
Answer by Lockstep · Jun 11, 2013 at 11:44 PM
You just have to remember if the character has already jumped midair:
public int airJumps = 1;
private int jumpCounter = 0;
//you could use a simple boolean instead, but
//like this you can do a triple, quadruple, etc jump
private bool isGrounded;
//The character controller script probably has this already
//Is true iff the character touches the ground
//inside Update:
if(isGrounded){
jumpCounter = 0;
}
if( Input.GetButton ("Jump") ){ //Lokate this in the script. It's probably already there in some form.
if(isGrounded){
Jump();
} else {
if(jumpCounter < airJumps){
jumpCounter++;
Jump();
}
}
}
Yeah, that seems like it should definitely work, though I'm a bit confused as to where I should apply this. Would it be in the Character $$anonymous$$otor script or the FPS Input Controller script?
Or would I want to do a completely separate script? I'm just a bit confused.
The motor script! If i recall it right, then the input controller only manages the input put none of the movement. The reason for this is, that the motot script is also meant to be useable for AI.
I know, that the motor script is a real beast. But consider, that it is able to manage moving platforms, slopes, eliptic speed calculation, smooth acceleration and more in addition to the usuall moving and jumping.
Your goal is to find the place where the Jump()
function gets executed: The easiest way to do this is using the search functionallity of your editor. I assume you use monodevelop since it comes with unity. Press control+F or select the icon in the toolbar, then type Jump in the search field. You can also select the function and use - find references - press shift + F12 or select it from the dropdown menu from the right mouse button.
The jump function needs to be executed somewhere in Update
( or FixedUpdate
). $$anonymous$$aybe it hides in another function inside of Update
. $$anonymous$$eep looking until you found it.
You will probably find it in such a context:
if(/*some statement that handles Input for jumping*/){ // in my example from above, this is Input.GetButton ("Jump"). It will probably look different.
Jump();
}
Then you need to replace the simple Jump()
with the block from my example, where we check for grounded or the jump counter.
You also need to make sure, that the original script does not prevent jumping when isGrounded is false. Search for isGrounded in the script and see where it restricts jumping. The only check (for jumping) must be the one from my example above.
Answer by superluigi · Jun 13, 2013 at 07:44 AM
sorry I'm writing this from my wiiU so I can't look at the controller script right now. Try this:
var jump : float = 9.0;
var dJump : float = 5.0;
var gravity : float = 20.0;
private var canDJump : boolean = false;
private var velocity : Vector3 = Vector3.zero;
var controller : CharacterController;
function Update()
{
if (!controller.isGrounded && canDJump && Input.GetButtonDown ("Jump"))
{
velocity.y = dJump;
canDJump = false;
}
if (controller.isGrounded)
{
if (Input.GetButtonDown ("Jump"))
{
velocity.y = jump;
canDJump = true;
}
}
velocity.y -= gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);
}
make sure the second jump(doubleJump) is on top of the firts jump in the code otherwise it will read both jumps in the same frame so you will jump twice real fast. By putting the double jump on top of the first jump you ensure that it will get read on the next frame so u can jump while in the air.
Your answer
Follow this Question
Related Questions
how to stop double jumping in 2d platform effector 0 Answers
Moving 3rd person player relative to orbiting camera 1 Answer
Troubles making custom 3D character controller (for platformer) 0 Answers
Objects won't disappear after a set time after collision 1 Answer
Need Some Help With Controlls. 1 Answer