Skip to content

Instantly share code, notes, and snippets.

View brihernandez's full-sized avatar

Brian Hernandez brihernandez

View GitHub Profile
@brihernandez
brihernandez / SASExample.cs
Created October 5, 2022 01:32
I wrote a Stability Augmentation System once. I forgot what I read that explained it, but I was really surprised how effective it was at stabilizing a Halo Pelican. I ended up not needing a PID for the stick and rudder, since this kept the oscillations under control.
// This is a basic dummy version of a Stability Augmentation System. There's a bunch of technical
// terms for this, but I'm not a control systems engineer so feel free to correct me. I found a
// really awesome document (video?) somewhere that explained all this stuff but it was a while ago
// but I forgot where I saw it.
// This is meant to be set up per channel. It's serializable so you can change
// the authority and power of it per channel in the Inspector.
[System.Serializable]
public class SASChannel
@brihernandez
brihernandez / NavMeshAreaIDs.cs
Last active June 20, 2022 19:07
Simple wrapper classes for handling Navmesh Area related operations in Unity.
using UnityEngine.AI;
/// <summary>
/// Convenience class for handling NavMesh Area related operations and lookups in Unity.
/// </summary>
public readonly struct UnityNavMeshLayer
{
/// <summary>
/// Name of the Area as defined in the Navigation window in Unity.
/// </summary>
@brihernandez
brihernandez / InterpolatedCurve.cs
Last active August 24, 2021 19:45
Like AnimationCurve, but dead simple and linear only. Is it really even a curve?
using System.Collections.Generic;
/// <summary>
/// Creates a very simple linear graph that can be sampled along arbitrary points. Clamps to
/// the nearest value when sampled out of range. This class assumes that all points given are
/// ALREADY SORTED from smallest to largest in the X axis. There is NO automatic sorting.
/// </summary>
public class InterpolatedCurve
{
public List<(float, float)> Points = new List<(float, float)>();
@brihernandez
brihernandez / SmoothDamp.cs
Last active January 10, 2024 08:05
Framerate independent damping
using UnityEngine;
// Thanks to Rory Driscoll
// http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/
public static class SmoothDamp
{
/// <summary>
/// Creates dampened motion between a and b that is framerate independent.
/// </summary>
/// <param name="from">Initial parameter</param>
@brihernandez
brihernandez / VectorFuryPlayerInput.cs
Last active April 18, 2022 19:47
Example of how to use the new Input System in Unity. This is how I handled input in Vector Fury.
// This is an example of how to use the new input system in a way that I think is actually
// usable for a production game, and not just rapid prototyping. This is much more stable,
// and because it uses the C# class version of the new Input System it will fail to compile
// if something changes in the input asset, which is a VERY GOOD THING.
using UnityEngine;
public class CameraInput
{
public float Pitch = 0f;
@brihernandez
brihernandez / FreelancerShipPhysics.cs
Last active August 11, 2023 03:51
Freelancer ship physics for Unity. Includes engine kill and thruster.
// ====================================================================================================
// In my codebase, all ships have their own instance of this that they use to apply physics with.
// I'm using a struct mostly because I want to maintain copy semantics.
// ====================================================================================================
public struct FlightInput
{
public float Pitch;
public float Yaw;
public float Roll;
@brihernandez
brihernandez / PixelPerfectTrail.cs
Last active April 25, 2022 21:51
TrailRenderer that maintains a width in screen space (URP)
public class PixelPerfectTrail : MonoBehaviour
{
[SerializeField] private TrailRenderer trail = null;
[Tooltip("How small the trail width is allowed to get in pixels.")]
public float MinimumPixelSize = 1.5f;
private float startSize = 1f;
private void Awake()
@brihernandez
brihernandez / ComputeGunLead.cs
Last active June 1, 2021 03:49
Computes a point for a gun to aim at in order to hit a target using linear prediction.
/// <summary>
/// Computes a point for a gun to aim at in order to hit a target using linear prediction.
/// Assumes a bullet with no gravity or drag. I.e. A bullet that maintains a constant
/// velocity after it's been fired.
/// </summary>
public static Vector3 ComputeGunLead(Vector3 targetPos, Vector3 targetVel, Vector3 ownPos, Vector3 ownVel, float muzzleVelocity)
{
// Extremely low muzzle velocities are unlikely to ever hit. This also prevents a
// possible division by zero if the muzzle velocity is zero for whatever reason.
if (muzzleVelocity < 1f)
@brihernandez
brihernandez / UGUIButtonExtensions.cs
Last active January 18, 2021 18:21
Set the text of a button in a more convenient and lazy way.
using UnityEngine.UI;
public static class UGUIButtonExtensions
{
public static void SetTextTMPro(this Button button, string text)
{
button.GetComponentInChildren<TMPro.TextMeshProUGUI>().text = text;
}
public static void SetTextUGUI(this Button button, string text)
@brihernandez
brihernandez / RevealTextWithDelay.cs
Last active January 9, 2021 16:07
Extension method to reveal TextMeshPro characters over time. Requires UniTask.
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using TMPro;
public static class TextMeshProExtensions
{
private static Dictionary<TextMeshProUGUI, CancellationTokenSource> textToCancel = new Dictionary<TextMeshProUGUI, CancellationTokenSource>();