Skip to content

Instantly share code, notes, and snippets.

@KellyThomas
Last active August 29, 2015 14:07
Show Gist options
  • Save KellyThomas/85f3cc473c76604314c6 to your computer and use it in GitHub Desktop.
Save KellyThomas/85f3cc473c76604314c6 to your computer and use it in GitHub Desktop.
CharacterController2D - partial class - Drop-through one way platforms
using UnityEngine;
/// <summary>
/// Extends the functionality of CharacterController2D to allow down jumps through one way platforms.
/// </summary>
public partial class CharacterController2D
{
/// <summary>
/// Stores a value indicating whether if this Controller should heed one way platforms
/// </summary>
[SerializeField]
private bool _oneWayPlatformsActive = true;
/// <summary>
/// Stores nominal value for platform mask
/// </summary>
private LayerMask _platformMaskNominal = 0;
/// <summary>
/// Stores nominal value for one way platform mask
/// </summary>
private LayerMask _oneWayPlatformMaskNominal = 0;
/// <summary>
/// Gets or sets a value indicating whether if this Controller should heed one way platforms
/// </summary>
public bool OneWayPlatformsActive
{
get
{
return _oneWayPlatformsActive;
}
set
{
_oneWayPlatformsActive = value;
UpdatePlatformMasks();
}
}
/// <summary>
/// Update platform masks to reflect current state of areOneWayPlatformsActive
/// </summary>
public void UpdatePlatformMasks()
{
if (OneWayPlatformsActive)
{
platformMask = _oneWayPlatformMaskNominal | _platformMaskNominal;
}
else
{
platformMask = _platformMaskNominal;
}
}
/// <summary>
/// Saves current value of platform masks as nominal values (replaces old nominal values)
/// </summary>
public void SavePlatformMasks()
{
_platformMaskNominal = platformMask;
_oneWayPlatformMaskNominal = oneWayPlatformMask;
}
}
/*
SETUP NOTES:
Some changes are required to CharacterController2D:
1. add keyword 'partial' to class declaration
2. add this line to Awake():
SavePlatformMasks()
USAGE NOTES:
Allow drop-though:
OneWayPlatformsActive = false;
Disable drop-though:
OneWayPlatformsActive = true;
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment