Skip to content

Input buffering for Unity

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

madeyellow/InputBuffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is it?

Simple yet effective package that buffers (aka remember) some input with 4 lines of code. Check if buffer isn't expired at any given moment, configure buffer's lifetime, cache input values, etc.

Where and why I should use it?

It's a perfect solution for buffering action input, such as pressing a jump button. If your player pressed it while not being grounded (e.g. right before landing) your logic may fail and player won't execute jump. Frustrating, right?

Getting started

First of all declare input buffer:

using MadeYellow.InputBuffer;


public SimpleInputBuffer jumpInputBuffer = new SimpleInputBuffer(); // Init input buffer

You may configure buffer hold time from Unity Editor by changing holdTime slider OR by passing holdTime argument to InputBuffer constructor.

You can choose between various types of InputBuffer:

  • SimpleInputBuffer - best for buffering button presses (in terms of Unity's InputSystem);
  • IntInputBuffer, FloatInputBuffer - best for buffering axis 1D input (in terms of Unity's InputSystem);
  • Vector2InputBuffer - best for buffering gamepad stick input or any other 2D input;

After you've set up suitable InputBuffer you should use .Set(); or .Set(value); method each time you want to buffer your input. You may do it like so:

jumpInputBuffer.Set(); // Set input buffer when input happened

When you need to check if certain input is buffered or not you can perform a simple check:

if (jumpInputBuffer.hasBuffer)
{
  jumpInputBuffer.Reset(); // Reset buffer since we most likely want to perform action required by input only once

  // Your code goes here, e.g. perform Jump action if input was buffered, etc.
}

TIP: It's best to perform those checks each frame in Update() or LateUpdate() method inside your MonoBehaviour, so you won't miss any input. It's super cheap in terms of performance.

How to install Trackable to my Unity project?

Use the Unity Package Manager (in Unity’s top menu: Window > Package Manager), click "+" icon, select "Add package from git URL" and type URL of this repository.

Get buffered value

If you created InputBuffer of any type except of SimpleInputBuffer (because it doesn't stores any value, only the fact of the input) you can call .value to retrieve current value of input.

var vectorBuffer = new Vector2InputBuffer();

vectorBuffer.Set(Vector2.one);

Debug.Log(vectorBuffer.value); // Will log (1.00, 1.00)

Recomendations

  • Don't reuse single InputBuffer instance for various input actions: one instance for jumping, another for dashing, etc;
  • Use .hasBuffer check each frame in order not to miss a frame where you might execute your buffered action;

Having trouble or a feature request?

Create an issue and I'll do my best to help you or add a new usefull feature to that package.

About

Input buffering for Unity

Topics

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages