Platformer Slide

This component is used to perform platformer slides.

By slide we mean slighlity faster movement than it’s normal movement respecting being on the ground. This if different from a dash because it can’t be performed on air and can only be executed horizontally. No vertical or diagonal movement allowed.

Category

Learnable

What it Implements

Interface

IPlatformerSlidePerformer

Setup

Inspector

  • Debug On - For visual feedback while detecting ceilings

  • Sliding Collider - It will be from this collider that the ability will check if it can stop sliding based on what is considered ceiling in order to prevent being stuck. You can also feed this via script

  • What is Ceiling - A LayerMask telling what is considered “ceiling”.

  • Colliders to Disable - An optional list of Collider2D that should be disabled while performing. Usefull when dealing with multiple colliders for your character.

  • Ceiling Detection Length - A float representing the lengh of the casted ray to detect if it is under anything considered “ceiling” before stoping the slide.

  • Perform Approach - You can mark Auto Perform checkbox and the component will perform automatically upon self resolving jump requests and stops. In case you are working with an FSM (we have one, checkout our FSM solution) you might want your state to call the Perform() and PerformExtraJump() methods.

Feeding Dependencies

Below you will find what you should feed this component with and how. You can always do it by directly accessing its methods or having components wich provide what they need and mark this component to seek for them through the inspector window.

Slide Handler

This component needs to told when to perform its slides. So you will need a component to do that.

You can use the Request(), Stop(), methods as seen below on the “How to use” section. You also can mark it to seek for an IPlatformerSlideHandler as long as it has some component wich implements it among its GameObject other components. If you opt to do so, the IPlatformerSlideHandler component will take care of calling the Request() and Stop() methods when needed.

Movement Performer

In order to move on the x while performing, this component will need a platformer movement performer.

You can directly set one: slide.MovementPerformer = movementPerformer. As long as movementPerformer implements the IPlatformerMovementPerformer interface. Or you can mark the component to seek for an IPlatformerMovementPerformer as long as it has some component wich implements it among its GameObject other components.

Our Platformer Dynamic Movement implements it.

Grounding

This component needs to know if its GameObject can be considered grounded to start and interrupt sliding.

You can use the UpdateGrounding(bool newGrounding) method every update or mark the component to seek for an IPlatformerGroundingProvider as long as it has some component wich implements it among its GameObject other components. Our Platformer Raycast Grounding Checker implements it.

Facing Direction

This component needs to know what direction its GameObject is facing in order to setup the slide correctly and move into the correct direction.

You can use the UpdateFacingDirectionSign(float newFacingDirectionSign) method every update or mark the component to seek for an IPlatformerFacingDirectionProvider as long as it has some component wich implements it among its GameObject other components. Our PlatformerFlip implements it.

Usage

Here is how you can use this component:

Direct approach

PlatformerSlide slide;

void Awake()
{
    slide = GetComponent<PlatformerSlide>();
}

void Update()
{
    if (Input.GetButtonDown("Slide")) // This assumes you've mapped a sliding button
    {
        slide.Request();
    }

    if (Input.GetButtonUp("Slide")) // This assumes you've mapped a sliding button
    {
        slide.Stop();
    }
}

void FixedUpdate()
{
    if (slide.sliding)  // In case it is not set to auto perform
    {
        slide.Perform();
    }
}

Requesting slides using IPlatformerSlidePerformer interface

IPlatformerSlidePerformer slidePerformer;

void Awake()
{
    slidePerformer = GetComponent<IPlatformerJumpPerformer>();
}

void Update()
{
    if (Input.GetButtonDown("Slide")) // Example on how to get input. We can't really know how is your input setup
    {
        slidePerformer.Request();
    }
}

void FixedUpdate()
{
    if (slidePerformer.Performing) // In case it is not set to auto perform
    {
        slidePerformer.Perform();
    }
}

Listening to IPlatformerSlidePerformer interface events

IPlatformerSlidePerformer slidePerformer;

void Awake()
{
    slidePerformer = GetComponent<IPlatformerSlidePerformer>();
}

void Start()
{
    slidePerformer?.SlideUpdate.AddListener(OnSlideUpdate);
}

void OnDisable()
{
    slidePerformer?.SlideUpdate.RemoveListener(OnSlideUpdate);
}

void OnSlideUpdate(bool isSliding)
{
    // Your logic
}

Note

You can also use the direct approach to listen to the events.

Locking Slides

Use the method Lock(bool shouldLock) to prevent or allow slides to be performed. Great to be used with FSMs when entering and leaving states on wich slides should not be started.

Available Properties

public bool Performing { get; }
public bool Locked { get; }

public IPlatformerSlideHandler SlideHandler { get; set; }
public IPlatformerMovementPerformer MovementPerformer { get; set; }
public IPlaftormerGroundingProvider GroundingProvider { get; set; }
public IPlatformerFacingDirectionProvider HorizontalDirectionProvider { get; set; }

public LayerMask WhatIsCeiling { get; set; }

Available Methods

/// <summary>
/// Call this to request a Slide.
/// </summary>
/// <param name="directionSign"></param>
public void Request(float directionSign)

/// <summary>
/// Should be called on Fixed (Physics) Update.
/// </summary>
public void Perform()

/// <summary>
/// Stops slide in progress if any.
/// </summary>
public void Stop()

/// <summary>
/// Call this in order to Lock slide and
/// prevent new slides to occur based on
/// shouldLock boolean.
/// </summary>
/// <param name="shouldLock"></param>
public void Lock(bool shouldLock)

/// <summary>
/// Call this to update grouding
/// </summary>
/// <param name="newGrounding"></param>
public void UpdateGronding(bool newGrounding)

/// <summary>
/// Call this to update facing direction sign. -1 for left, 1 for right.
/// </summary>
/// <param name="newFacingDirectionSign"></param>
public void UpdateFacingDirectionSign(float newFacingDirectionSign)