[Unity Coding] Debug Physics2D.BoxCast

Physics2D.BoxCast(origin, size, angle, direction, distance)

Vector2 t_tL = origin+ new Vector2(-size.x * 0.5f, size.y * 0.5f);
Vector2 t_tR = origin+ new Vector2(size.x * 0.5f, size.y * 0.5f);
Vector2 t_bL = origin+ new Vector2(-size.x * 0.5f, -size.y * 0.5f);
Vector2 t_bR = origin+ new Vector2(size.x * 0.5f, -size.y * 0.5f);
Color t_lineColor = Color.cyan;
Debug.DrawLine(t_tL, t_tR, t_lineColor);
Debug.DrawLine(t_bL, t_bR, t_lineColor);
Debug.DrawLine(t_tL, t_bL, t_lineColor);
Debug.DrawLine(t_tR, t_bR, t_lineColor);

[Unity C#] Notes for ESC

Components

using Entitas;
using UnityEngine;

public class PositionComponent : IComponent {
	public Vector3 value;
}

public class HealthComponent : IComponent {
	public float value;
}

public sealed class DestroyedComponent : IComponent {}

InitializeSystem

public sealed class CreatePlayerSystem : IInitializeSystem {

	readonly Contexts _contexts;

	public CreatePlayerSystem (Contexts g_contexts) {
		_contexts = g_contexts;
	}

	public void Initialize () {
		GameEntity t_entity = _contexts.game.CreateEntity ();
		t_entity.AddHealth (100);
	}
}

ExecuteSystem

public sealed class LogHealthSystem : IExecuteSystem {

	readonly IGroup<GameEntity> _entities;

	public LogHealthSystem (Contexts contexts) {
		_entities = contexts.game.GetGroup (GameMatcher.Health);
	}

	public void Execute () {
		foreach (GameEntity f_entity in _entities) {
			float f_health = f_entity.health.value;
			UnityEngine.Debug.Log ("health: " + f_health);
		}
	}

}

ReactiveSystem

public sealed class HealthSystem : ReactiveSystem<GameEntity> {
	public HealthSystem (Contexts contexts) : base (contexts.game) {
	}

	// trigger for certain component
	protected override ICollector<GameEntity> GetTrigger (IContext<GameEntity> context) {
		return context.CreateCollector (GameMatcher.Health);
	}

	// filter for things you want to check
	protected override bool Filter (GameEntity entity) {
		return entity.hasHealth;
	}

	protected override void Execute (List<GameEntity> entities) {
		foreach (GameEntity f_entity in entities) {
			if (f_entity.health.value <= 0)
				f_entity.isDestroyed = true;
		}
	}

}

A Root for All Systems

public class RootSystems : Feature {
	public RootSystems (Contexts g_contexts) {
		Add (new CreatePlayerSystem (g_contexts));
		Add (new LogHealthSystem (g_contexts));
		Add (new HealthSystem (g_contexts));
		Add (new DestroyEntitySystem (g_contexts));
	}
	
}

To Call Systems

	RootSystems _systems;

	void Start () {
		//Contexts t_contexts = new Contexts ();
		//Contexts otherWayToGetContexts = Contexts.sharedInstance;

		_systems = new RootSystems (Contexts.sharedInstance);

		_systems.Initialize ();
	}

	private void Update () {
		_systems.Execute ();
	}

[Unity Reminder] Quaternion

Quaternion.operator *
1) public static Quaternion operator *(Quaternion lhs, Quaternion rhs);
– Combines rotations lhs and rhs.
2) public static Vector3 operator *(Quaternion g_rotation, Vector3 g_point);
– Rotates the point g_point with g_rotation.

[Unity Reminder] Tips

OnMouseDown ()
When Parent Object has rigidbody on it, the OnMouseDown () function cannot be detected on the Child Object.

Skybox
Texture “Wrap Mode” should be “Clamp”

[Unity C#] Raycast & LayerMask

float myVisionDistance = 50;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
int t_layerMask = (int) Mathf.Pow (2, 8); //for the layer you want to do the raycast
if (Physics.Raycast (ray, out hit, myVisionDistance, t_layerMask))
if (hit.collider.tag == "Food") {
    myTargetPosition = hit.transform.position;
}

Calculate layerMask:
If you want to do the raycast on layer 1,4&8,

layerMask = (int) (
    Mathf.Pow (2, 1) +
    Mathf.Pow (2, 4) +
    Mathf.Pow (2, 8)
);

[Unity C#] Create Grids in Unity Editor

//Created by Tim & Hang
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class CS_GridSetup : MonoBehaviour {

    [SerializeField] bool createGrids = false;

    [SerializeField] GameObject myGridPrefab;

    [SerializeField] int myColumns = 16;
    [SerializeField] int myRows = 9;

    [SerializeField] Vector2 myBottomLeft = new Vector2 (-8, -4);
    [SerializeField] Vector2 myTopRight = new Vector2 (8, 4);
    [SerializeField] float myPositionZ = 10;

    private List<GameObject> myGrids = new List<GameObject> ();

    // Update is called when something changes in the scene
    void Update () {
        if (createGrids == true) {
            createGrids = false;

            //Clear the existing grids

            foreach (GameObject t_grid in myGrids) {
                GameObject.DestroyImmediate (t_grid);
            }
            myGrids.Clear ();

            //create grids

            for (int i = 0; i < myRows; i++) {
                for (int j = 0; j < myColumns; j++) {
                    Vector3 t_position = new Vector3 (
                                             (myTopRight.x - myBottomLeft.x) / (myColumns - 1) * j + myBottomLeft.x,
                                             (myTopRight.y - myBottomLeft.y) / (myRows - 1) * i + myBottomLeft.y,
                                             myPositionZ); //Calculate the position
                    GameObject t_grid = Instantiate (myGridPrefab, t_position, Quaternion.identity); //Create the grid
                    myGrids.Add (t_grid); //Add it to the list
                    t_grid.transform.SetParent (this.transform); //Set the parent to this gameObject
                    t_grid.name = myGridPrefab.name + "(" + i + ")(" + j + ")"; //Name it
                }
            }
        }
    }
}

[Unity Shader] The Shader in SecantRemix

Here is the link to the game – SecantRemix:
https://turist.itch.io/secant-remix

Here is the tutorial of replacement shaders:

Here is the shader we used in the game:

Shader "Hidden/Show Depth";
{
    Properties
    {
        _Color("Color", Color) = (1,1,1,1)
    }

    SubShader
    {
        Tags
        {
            "RenderType"="Opaque"
        }

        ZWrite On

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float depth : DEPTH;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.depth = -mul(UNITY_MATRIX_MV, v.vertex).z *_ProjectionParams.w;
                return o;
            }

            half4 _Color;

            half4 _StartColor;
            half4 _MidColor;
            half4 _EndColor;

            fixed4 frag (v2f i) : SV_Target
            {
                return (max (0, 1 - i.depth * 2) * _StartColor + (0.5 - abs (0.5 - i.depth)) * 2 * _MidColor + max (0, i.depth * 2 - 1) * _EndColor) * _Color;
            }
            ENDCG
        }
    }
}

Also, you need to attach this script to your camera:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[ExecuteInEditMode]
public class ReplacementShaderEffect : MonoBehaviour {
    [SerializeField] Shader ReplacementShader; // the shader above
    [SerializeField] Color startColor;
    [SerializeField] Color midColor;
    [SerializeField] Color endColor;

    Camera mainCam;

    private Color startColorTarget;
    private Color midColorTarget;
    private Color endColorTarget;
    [SerializeField] float myColorChangeSpeed = 1;

    void Start() {
        startColorTarget = startColor;
        midColorTarget = midColor;
        endColorTarget = endColor;

        mainCam = GetComponent ();
        Shader.SetGlobalColor("_StartColor", startColor);
        Shader.SetGlobalColor("_MidColor", midColor);
        Shader.SetGlobalColor("_EndColor", endColor);

    }

    void Update() { // make background color always the same as end color in shader

        mainCam.backgroundColor = Color.Lerp (endColor, startColorTarget, Time.deltaTime * myColorChangeSpeed);

        startColor = Color.Lerp (startColor, startColorTarget, Time.deltaTime * myColorChangeSpeed);
        midColor = Color.Lerp (midColor, midColorTarget, Time.deltaTime * myColorChangeSpeed);
        endColor = Color.Lerp (endColor, endColorTarget, Time.deltaTime * myColorChangeSpeed);

        Shader.SetGlobalColor("_StartColor", startColor);
        Shader.SetGlobalColor("_MidColor", midColor);
        Shader.SetGlobalColor("_EndColor", endColor);
    }

    void OnValidate() {
        Shader.SetGlobalColor("_StartColor", startColor);
        Shader.SetGlobalColor("_MidColor", midColor);
        Shader.SetGlobalColor("_EndColor", endColor);

    }

    void OnEnable() {
        if (ReplacementShader != null)
            GetComponent().SetReplacementShader(ReplacementShader, "RenderType");
    }

    void OnDisable() {
        GetComponent().ResetReplacementShader();
    }

    void ChangeStartColor (Color newStartColor) {
        startColorTarget = newStartColor;
    }

    void ChangeMidColor (Color newMidColor) {
        midColorTarget = newMidColor;
    }

    void ChangeEndColor (Color newEndColor) {
        endColorTarget = newEndColor;
    }

    public void ChangeColors (Color g_start, Color g_mid, Color g_end) {
        ChangeStartColor (g_start);
        ChangeMidColor (g_mid);
        ChangeEndColor (g_end);
        Debug.Log ("ChangeColors");
    }

}

Here is the result: