Skip to content

Instantly share code, notes, and snippets.

View beardordie's full-sized avatar

Beard or Die beardordie

View GitHub Profile
@beardordie
beardordie / DebugText.cs
Last active August 28, 2021 15:36
Script component to add to GameObjects to easily show debug text above within Scene view
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>DebugText class
/// AUTHOR: Beard or Die (more or less)
/// BASED ON AND THANKS TO: Video and code by Unity3d.college: https://www.youtube.com/watch?v=uFFexymk3FY
/// DATE: 2018-01-11
/// COPYRIGHT: None; even remove this text if you want.
/// PURPOSE:
@beardordie
beardordie / GhostFreeRoamCamera3D.cs
Created January 12, 2018 08:14
Unity Camera Controller for easy FPS-style navigation in 3D scenes, or Editor Scene-view style
using UnityEngine;
/// <summary>GhostFreeRoamCamera3D class
/// Based on free asset "Ghost Free Roam Camera" by Goosey Gamez
/// https://www.assetstore.unity3d.com/en/#!/content/19250
/// That asset has no copyright or license info in its readme and modifications are encouraged
/// Modified by: Beard or Die
/// Date: 2018-01-12
/// GOAL: A quick camera controller for debugging 3D scenes
/// Flexible enough for FPS-style movement (default), or Unity Editor-style movement
@beardordie
beardordie / SceneViewCamera3D.cs
Created January 12, 2018 08:15
Unity Camera Controller for easy Editor Scene-view style navigation in 3D scenes, or FPS-style
using UnityEngine;
/// <summary>SceneViewCamera3D class
/// Based on free asset "Ghost Free Roam Camera" by Goosey Gamez
/// https://www.assetstore.unity3d.com/en/#!/content/19250
/// That asset has no copyright or license info in its readme and modifications are encouraged
/// Modified by: Beard or Die
/// Date: 2018-01-12
/// GOAL: A quick camera controller for debugging 3D scenes
/// Flexible enough for FPS-style movement, or Unity Editor-style movement (default)
@beardordie
beardordie / My2DExtensions.cs
Created January 12, 2018 22:49
Unity GameObject Extensions for human-readable code when working with 2D transform positions
public static class My2DExtensions {
public static bool IsLeftOf(this GameObject t1, GameObject t2)
{
if(t1.transform.position.x < t2.transform.position.x){ return true; } else { return false; })
}
public static bool IsRightOf(this GameObject t1, GameObject t2)
{
if(t1.transform.position.x < t2.transform.position.x){ return true; } else { return false; }
}
public static bool IsCenterOf(this GameObject t1, GameObject t2, float wiggleRoom=0f)
@beardordie
beardordie / DebugTextGameView.cs
Created January 13, 2018 01:52
Unity script component to show debug text on your GameObjects when you need to see it in Game view, not just Scene view.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>DebugTextGameView class
/// AUTHOR: Beard or Die
/// DATE: 2018-01-12
/// REQUIREMENTS: Add this script onto a GameObject that you want some debug text to
/// appear over in Game View. Set up in inspector. You will also need to create
@beardordie
beardordie / LayerMaskCheck.txt
Created August 8, 2018 22:08
LayerMask check
LayerMasks are used often in Unity Technologies' 3D Game Kit to ensure a given interaction is only permitted/triggered by allowed objects.
Here are some code snippets they use often in the kit to achieve that check:
public LayerMask layers;
void OnTriggerEnter(Collider other)
{
if (0 != (layers.value & 1 << other.gameObject.layer))
{
// layerMask check is met. Do stuff now.
}
@beardordie
beardordie / NestedClassHolder.cs
Created August 8, 2018 22:36
Nested Classes
// Use a class that does not inherit from MonoBehaviour and has attribute [System.Serializable]
// then reference that in a MonoBehaviour class. This results in a dropdown in the Inspector
// for the referenced class.
// The documentation says to have the NestedClass inherit from System.Object but I've seen no need for that.
// For more robust functionality, consider creating a ScriptableObject/template for the Nested Classes,
// then reference those. It won't drop down in the inspector the same way, but it affords other benefits.
using UnityEngine;
public class NestedClassHolder : MonoBehaviour
{
@beardordie
beardordie / HowTo-UnityUICamera-AlwaysOnTop.txt
Created August 18, 2018 03:54
Unity UI Camera - Always On Top - Setup Instructions
If you would like to have some UI elements in world space, but always appear on "top" (never obscured by other world objects), here is the setup:
- Create a new camera as a child of your main camera, this is your UI camera
- Zero out the new camera's transform position, and match its FOV with the parent camera
- Set the Layer of this camera to UI
- Set Clear Flags to Depth only
- Set Culling mask to none then to only the UI layer
- On your main camera, set Culling mask to omit the UI layer
After doing this, any canvas or UI elements on that canvas set to the UI layer will only use the UI camera to render, and will not be hidden by intersecting world objects. i.e. they will be Always On Top
@beardordie
beardordie / SimpleAudioEvent.cs
Last active June 11, 2020 18:53
Simple Audio Event - supports multiple clips per event, pitch and volume variance - with Custom Editor
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Audio;
/// <summary>
/// This is a template to create a new Simple Audio Event.
/// It supports an array of audio clips, pitch and volume variance,
/// and a custom editor that shows a button to preview a random sound
/// from the array with the selected pitch and volume variance.
@beardordie
beardordie / RangedFloat.cs
Created August 18, 2018 19:16
RangedFloat type with Custom Editor
using System;
[Serializable]
public struct RangedFloat
{
public float minValue;
public float maxValue;
public RangedFloat(float minValue, float maxValue) //constructor
{