cjddmut / Unity-2D-Platformer-Controller

A customizable 2D platformer motor that handles mechanics such as double jumps, wall jumps, and corner grabs. Includes a player controlled prefab that can be dropped into any scene for immediate support.
MIT License
873 stars 163 forks source link

Tap for small jump? #67

Open digiwombat opened 7 years ago

digiwombat commented 7 years ago

Currently, you can only full jump or hold for really awkward extra height (since holding just turns off gravity, I believe).

I'd like to be able to tap for a small jump and hold for the full height, rather than get EXTRA height (which comes on awkwardly if you set a small minimum jump (0.1f) and a larger full height (4f).

Basically, look at Metroid or Castlevania and you can tap for a small jump rather than quick tap giving you full height.

digiwombat commented 7 years ago

Alright, I implemented this roughly for now in my own instance. I'll explain it here for people looking to do the same thing.

Firstly add two variables to PlayerMotor2D.cs:

public float minJumpHeight = 0.5f;
public bool endJumpEarly = false;

Then add this to MoveMotor() (also in PlayerMotor2D.cs)

if (endJumpEarly && motorState != MotorState.OnGround)
{
    if (_velocity.y > CalculateSpeedNeeded(minJumpHeight))
    {
        _velocity.y = CalculateSpeedNeeded(minJumpHeight);
    }
}

Finally! In PlayerController2D.cs (or your local equivalent) add the following to Update():

if(_motor.motorState != PlatformerMotor2D.MotorState.Jumping)
{
    _motor.endJumpEarly = false;
}

if (Input.GetAxis(PC2D.Input.VERTICAL) > -0.5f && Input.GetButtonDown("Jump"))
{
    _motor.Jump();
    _motor.DisableRestrictedArea();
}

if(Input.GetButtonUp("Jump"))
{
    _motor.endJumpEarly = true;
}

That'll do it. There's probably a smarter way to set endJumpEarly, but for now it'll do for double jumps and the like. The onGround check might honestly be redundant, but I figure passing over the second, more complicated if() statement is probably for the best.

EDIT (13 May): Changed the endJumpEarly thing to what I originally had it as. Might interfere with double jumps. Maybe. Somehow.