[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 Reminder] Collider

void OnCollisionEnter (Collision collision)
void OnCollisionStay (Collision collision)
void OnCollisionExit (Collision collision)

void OnTriggerEnter(Collider other)
void OnTriggerStay(Collider other)
void OnTriggerExit(Collider other)

void OnCollisionEnter2D (Collision2D coll)
void OnCollisionStay2D (Collision2D coll)
void OnCollisionExit2D (Collision2D coll)

void OnTriggerEnter2D (Collider2D other)
void OnTriggerStay2D (Collider2D other)
void OnTriggerExit2D (Collider2D other)

[Unity C#] Create Grids in Unity Editor

[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 C#] The Shader in SecantRemix

[Unity C#] 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 &amp;quot;Hidden/Show Depth&amp;quot;
{
    Properties
    {
        _Color(&amp;quot;Color&amp;quot;, Color) = (1,1,1,1)
    }

    SubShader
    {
        Tags
        {
            &amp;quot;RenderType&amp;quot;=&amp;quot;Opaque&amp;quot;
        }

        ZWrite On

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include &amp;quot;UnityCG.cginc&amp;quot;

            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(&amp;quot;_StartColor&amp;quot;, startColor);
        Shader.SetGlobalColor(&amp;quot;_MidColor&amp;quot;, midColor);
        Shader.SetGlobalColor(&amp;quot;_EndColor&amp;quot;, 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(&amp;quot;_StartColor&amp;quot;, startColor);
        Shader.SetGlobalColor(&amp;quot;_MidColor&amp;quot;, midColor);
        Shader.SetGlobalColor(&amp;quot;_EndColor&amp;quot;, endColor);
    }

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

    }

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

    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 (&amp;quot;ChangeColors&amp;quot;);
    }

}

Here is the result:

1

[Unity C#] Single Instance

When there should be only one instance of this class in the whole game that shouldn’t be destroyed.

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

public class CS_MessageBox : MonoBehaviour {

    private static CS_MessageBox instance = null;

    //========================================================================
    public static CS_MessageBox Instance {
        get { 
            return instance;
        }
    }

    void Awake () {
        if (instance != null && instance != this) {
            Destroy(this.gameObject);
        } else {
            instance = this;
        }
        DontDestroyOnLoad(this.gameObject);
    }
    //========================================================================

    public void FunctionYouNeed () {
        Debug.Log("FunctionYouNeed is called :)");
    }
}

To call this instance

CS_MessageBox.Instance.FunctionYouNeed();

[Unity C#] Serializable Class

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

[System.Serializable]
public class CS_AudioClip {
    public AudioClip[] myAudioClips;
    public float myVolum = 1;
    public float myPitchDifference = 0;

    public AudioClip GetMyAudioClip () {
        if (myAudioClips.Length &lt;= 0)
            return null;
        return myAudioClips [Random.Range (0, myAudioClips.Length)];
    }

    public float GetMyPitch () {
        return (1 + myPitchDifference * Random.Range (0, 1.0f));
    }
}

to use this class in other scripts:

public CS_AudioClip myMoveSFX;