[RELEASED] Easy Character Movement 2

Hi @desukarhu ,

For this, I suggest creating a custom character class (extending the Character class) and overriding its GetMaxSpeed method. This will allow us to apply a modifier to the actual character speed (e.g., walking, falling, crouching, etc.). In our case, it will be a directional movement modifier. For example:

public float forwardSpeedMultiplier = 1.0f
public float strafeSpeedMultiplier = 0.75f;
public float backwardSpeedMultiplier = 0.5f;

protected float CalculateDirectionalModifier()
{
    // Compute planar move direction

    Vector3 characterUp = GetUpVector();
    Vector3 planarMoveDirection = Vector3.ProjectOnPlane(GetMovementDirection(), characterUp);

    // Compute actual walk speed factoring movement direction

    Vector3 characterForward = Vector3.ProjectOnPlane(GetForwardVector(), characterUp).normalized;

    // Compute final directional modifier

    float forwardMovement = Vector3.Dot(planarMoveDirection, characterForward);

    float speedMultiplier = forwardMovement >= 0.0f
        ? Mathf.Lerp(strafeSpeedMultiplier, forwardSpeedMultiplier, forwardMovement)
        : Mathf.Lerp(strafeSpeedMultiplier, backwardSpeedMultiplier, -forwardMovement);

    return speedMultiplier;
}

public override float GetMaxSpeed()
{
    float actualMaxSpeed = base.GetMaxSpeed();
    return actualMaxSpeed * CalculateDirectionalModifier();
}

The CalculateDirectionalModifier function uses the dot product to determine how much the character is moving along its forward vector. It later uses this value (ranging from -1 to 1) to compute the final movement modifier. Finally, this speed modifier is applied in the Character’s GetMaxSpeed method, returning the final modified speed.

2 Likes