Posted on

Among Us in Unity – Reporting Bodies (Lesson 8)

For this lesson on how to make Among Us in Unity, we will go over how to create the report mechanic. This makes it so the player can press a button when they see a dead body and a message will then be sent to report the body.

Unlock Code and Member Content

AU_PlayerController.cs

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

public class AU_PlayerController : MonoBehaviour
{
    [SerializeField] bool hasControl;
    public static AU_PlayerController localPlayer;
    

    //Components
    Rigidbody myRB;
    Animator myAnim;
    Transform myAvatar;
    //Player movement
    [SerializeField] InputAction WASD;
    Vector2 movementInput;
    [SerializeField] float movementSpeed;
    //Player Color
    [SerializeField] Color myColor;
    SpriteRenderer myAvatarSprite;

    //Role
    [SerializeField] bool isImposter;
    [SerializeField] InputAction KILL;
    float killInput;

    List<AU_PlayerController> targets;
    [SerializeField] Collider myCollider;

    bool isDead;

    [SerializeField] GameObject bodyPrefab;

    public static List<Transform> allBodies;

    List<Transform> bodiesFound;

    [SerializeField] InputAction REPORT;
    [SerializeField] LayerMask ignoreForBody;

    private void Awake()
    {
        KILL.performed += KillTarget;
        REPORT.performed += ReportBody;
    }

     

    private void OnEnable()
    {
        WASD.Enable();
        KILL.Enable();
        REPORT.Enable();

    }

    private void OnDisable()
    {
        WASD.Disable();
        KILL.Disable();
        REPORT.Disable();

    }


    // Start is called before the first frame update
    void Start()
    {
        if(hasControl)
        {
            localPlayer = this;
        }
        
        targets = new List<AU_PlayerController>();
        myRB = GetComponent<Rigidbody>();
        myAnim = GetComponent<Animator>();
        myAvatar = transform.GetChild(0);
        myAvatarSprite = myAvatar.GetComponent<SpriteRenderer>();
        if (!hasControl)
            return;
        if (myColor == Color.clear)
            myColor = Color.white;
        myAvatarSprite.color = myColor;

       
        allBodies = new List<Transform>();

        bodiesFound = new List<Transform>();
    }

    // Update is called once per frame
    void Update()
    {
        if (!hasControl)
            return;

        movementInput = WASD.ReadValue<Vector2>();
        myAnim.SetFloat("Speed", movementInput.magnitude);
        if (movementInput.x != 0)
        {
            myAvatar.localScale = new Vector2(Mathf.Sign(movementInput.x), 1);
        }



        if(allBodies.Count > 0)
        {
            BodySearch();
        }

    }

    private void FixedUpdate()
    {
        myRB.velocity = movementInput * movementSpeed;
    }

    public void SetColor(Color newColor)
    {
        myColor = newColor;
        if (myAvatarSprite != null)
        {
            myAvatarSprite.color = myColor;
        }
    }

    public void SetRole(bool newRole)
    {
        isImposter = newRole;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            AU_PlayerController tempTarget = other.GetComponent<AU_PlayerController>();
            if (isImposter)
            {
                if (tempTarget.isImposter)
                    return;
                else
                {
                    targets.Add(tempTarget);
                    
                }
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.tag == "Player")
        {
            AU_PlayerController tempTarget = other.GetComponent<AU_PlayerController>();
            if (targets.Contains(tempTarget))
            {
                    targets.Remove(tempTarget);
            }
        }
    }

    private void KillTarget(InputAction.CallbackContext context)
    {
        if (context.phase == InputActionPhase.Performed)
        {
            //Debug.Log(targets.Count);
            if (targets.Count == 0)
                return;
            else
            {

                if (targets[targets.Count - 1].isDead)
                    return;

                transform.position = targets[targets.Count - 1].transform.position;
                targets[targets.Count - 1].Die();
                targets.RemoveAt(targets.Count - 1);
            }
        }
    }

    public void Die()
    {
        AU_Body tempBody = Instantiate(bodyPrefab, transform.position, transform.rotation).GetComponent<AU_Body>();
        tempBody.SetColor(myAvatarSprite.color);

        isDead = true;

        myAnim.SetBool("IsDead", isDead);
        gameObject.layer = 9;
        myCollider.enabled = false;
    }

    void BodySearch()
    {
        foreach(Transform body in allBodies)
        {
            RaycastHit hit;
            Ray ray = new Ray(transform.position, body.position - transform.position);
            Debug.DrawRay(transform.position, body.position - transform.position, Color.cyan);
            if(Physics.Raycast(ray, out hit, 1000f, ~ignoreForBody))
            {
                
                if (hit.transform == body)
                {
                    //Debug.Log(hit.transform.name);
                    //Debug.Log(bodiesFound.Count);
                    if (bodiesFound.Contains(body.transform))
                        return;
                    bodiesFound.Add(body.transform);
                }
                else
                {
                    
                    bodiesFound.Remove(body.transform);
                }
            }
        }
    }

    private void ReportBody(InputAction.CallbackContext obj)
    {
        if (bodiesFound == null)
            return;
        if (bodiesFound.Count == 0)
            return;
        Transform tempBody = bodiesFound[bodiesFound.Count - 1];
        allBodies.Remove(tempBody);
        bodiesFound.Remove(tempBody);
        tempBody.GetComponent<AU_Body>().Report();
    }

}

AU_Body.cs

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

public class AU_Body : MonoBehaviour
{
    [SerializeField] SpriteRenderer bodySprite;

    public void SetColor(Color newColor)
    {
        bodySprite.color = newColor;
    }

    private void OnEnable()
    {
        if(AU_PlayerController.allBodies != null)
        {
            AU_PlayerController.allBodies.Add(transform);
        }
    }

    public void Report()
    {
        Debug.Log("Reported");
        Destroy(gameObject);
    }
}
using Photon.Chat;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PhotonChatManager : MonoBehaviour, IChatClientListener
{
    #region Setup

    [SerializeField] GameObject joinChatButton;
    ChatClient chatClient;
    bool isConnected;
    [SerializeField] string username;

    public void UsernameOnValueChange(string valueIn)
    {
        username = valueIn;
    }

    public void ChatConnectOnClick()
    {
        isConnected = true;
        chatClient = new ChatClient(this);
        //chatClient.ChatRegion = "US";
        chatClient.Connect(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat, PhotonNetwork.AppVersion, new AuthenticationValues(username));
        Debug.Log("Connenting");
    }

    #endregion Setup

    #region General

    [SerializeField] GameObject chatPanel;
    string privateReceiver = "";
    string currentChat;
    [SerializeField] InputField chatField;
    [SerializeField] Text chatDisplay;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (isConnected)
        {
            chatClient.Service();
        }

        if (chatField.text != "" &amp;&amp; Input.GetKey(KeyCode.Return))
        {
            SubmitPublicChatOnClick();
            SubmitPrivateChatOnClick();
        }
    }

    #endregion General

    #region PublicChat

    public void SubmitPublicChatOnClick()
    {
        if (privateReceiver == "")
        {
            chatClient.PublishMessage("RegionChannel", currentChat);
            chatField.text = "";
            currentChat = "";
        }
    }

    public void TypeChatOnValueChange(string valueIn)
    {
        currentChat = valueIn;
    }

    #endregion PublicChat

    #region PrivateChat

    public void ReceiverOnValueChange(string valueIn)
    {
        privateReceiver = valueIn;
    }

    public void SubmitPrivateChatOnClick()
    {
        if (privateReceiver != "")
        {
            chatClient.SendPrivateMessage(privateReceiver, currentChat);
            chatField.text = "";
            currentChat = "";
        }
    }

    #endregion PrivateChat

    #region Callbacks

    public void DebugReturn(DebugLevel level, string message)
    {
        //throw new System.NotImplementedException();
    }

    public void OnChatStateChange(ChatState state)
    {
        if(state == ChatState.Uninitialized)
        {
            isConnected = false;
            joinChatButton.SetActive(true);
            chatPanel.SetActive(false);
        }
    }

    public void OnConnected()
    {
        Debug.Log("Connected");
        joinChatButton.SetActive(false);
        chatClient.Subscribe(new string[] { "RegionChannel" });
    }

    public void OnDisconnected()
    {
        isConnected = false;
        joinChatButton.SetActive(true);
        chatPanel.SetActive(false);
    }

    public void OnGetMessages(string channelName, string[] senders, object[] messages)
    {
        string msgs = "";
        for (int i = 0; i &lt; senders.Length; i++)
        {
            msgs = string.Format("{0}: {1}", senders[i], messages[i]);

            chatDisplay.text += "\n" + msgs;

            Debug.Log(msgs);
        }

    }

    public void OnPrivateMessage(string sender, object message, string channelName)
    {
        string msgs = "";

        msgs = string.Format("(Private) {0}: {1}", sender, message);

        chatDisplay.text += "\n " + msgs;

        Debug.Log(msgs);
        
    }

    public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
    {
        throw new System.NotImplementedException();
    }

    public void OnSubscribed(string[] channels, bool[] results)
    {
        chatPanel.SetActive(true);
    }

    public void OnUnsubscribed(string[] channels)
    {
        throw new System.NotImplementedException();
    }

    public void OnUserSubscribed(string channel, string user)
    {
        throw new System.NotImplementedException();
    }

    public void OnUserUnsubscribed(string channel, string user)
    {
        throw new System.NotImplementedException();
    }

    #endregion Callbacks
}
Posted on

Among Us in Unity – 2D Lighting and Universal Render Pipeline (Bonus Lesson)

In this lesson on how to create Among Us in Unity, I will show you how to get the same lighting effect as what we created in the last lesson only this time we will use the new 2D lights from the Universal Render Pipeline. Using this option is good because it is much more precise and optimized. The downside to using this system is that it is still listed as Experimental and there is no real way that I know of to mask the players in the shadows without turning the shadow intensity up all the way.

The first thing that we will do in our project is to reset the hierarchy to the way it was before the last video. We will then need to implement the Universal Render Pipeline. first, install the package from the package manager. Then you will need to create a Pipeline Asset and a 2D renderer. After which you will need to apply the 2D renderer to the pipeline asset and finally you will need to apply the pipeline to the graphics setting in the project settings.

Next, we can add a 2D point light to our scene which I have added our player prefab. the Final thing that you will need to do is add Shadow Caster scripts to our wall objects.

Posted on

Among Us in Unity – Field of View and Shadow Casting (Lesson 7)

Welcome to this very exciting lesson on how to create Among Us in Unity. For this lesson, we will be creating the Field of View or Shadow Casting system. This makes it so you can only see players and objects within your line of sight.

LightCasting script on GitHub https://github.com/ckawell/LightShafting

We will make a few modifications to this script and then we will apply it to our player prefab. To set up the scene we will need to have to layers one for things that are shown in the shadows and one for things that are revealed in the light. We will then need to have two cameras one for the dark and one for the light. This is done by setting the Culling Masks of the camera. After which we will need to create a render texture for capturing the view of our light camera. This render texture will then be applied through a material onto a mesh in our scene. Finally, we will use the LightCaster script to dynamically update our render texture mesh to fit the visible space around our player.

Shader Script: https://gamedev.stackexchange.com/questions/109810/masking-with-3d-object-in-unity

lightcaster.cs

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

public class lightcaster : MonoBehaviour
{
    [SerializeField] LayerMask ignoreMe;
    [SerializeField] float getRadius;
    [SerializeField] LayerMask wallMask;

	public Collider [] sceneObjects; //The objects in the scene to effect the lighting.

	private Mesh mesh; //The light mesh.

    public GameObject lightRays; //the light game object.

    public float offset = 0.0001f; //the offset of the two rays cast to the left and right of each vertex of the scene objects.

    public bool showRed; //For debugging: shows the red rays casted (the negative offset rays).
    public bool showGreen; //For debugging: shows the green rays casted (the positive offset rays).

    public struct angledVerts{ //used for updating the vertices and UVs of the light mesh. The angle variable is for properly sorting the ray hit points.
        public Vector3 vert;
        public float angle;
        public Vector2 uv;
    }

	// Use this for initialization
	void Start () {
		mesh = lightRays.GetComponent<MeshFilter>().mesh; //inits the mesh of the light.
	}


    /// <summary>
    /// Adds three ints to the end of an int array.
    /// </summary>
    /// <param name="original"></param>
    /// <param name="itemToAdd1"></param>
    /// <param name="itemToAdd2"></param>
    /// <param name="itemToAdd3"></param>
    /// <returns></returns>
	public static int[] AddItemsToArray (int[] original, int itemToAdd1, int itemToAdd2, int itemToAdd3) {
      int[] finalArray = new int[ original.Length + 3 ];
      for(int i = 0; i < original.Length; i ++ ) {
           finalArray[i] = original[i];
      }
      finalArray[original.Length] = itemToAdd1;
      finalArray[original.Length + 1] = itemToAdd2;
      finalArray[original.Length + 2] = itemToAdd3;
      return finalArray;
 	}

    /// <summary>
    /// Adds two arrays together, making a third array.
    /// </summary>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <returns></returns>
    public static Vector3[] ConcatArrays(Vector3[] first, Vector3[] second){
        Vector3[] concatted = new Vector3[first.Length + second.Length];

        Array.Copy(first, concatted, first.Length);
        Array.Copy(second, 0, concatted, first.Length, second.Length);

        return concatted;
     }

	// Update is called once per frame
	void Update()
    {
        GetWalls();
		mesh.Clear(); //clears the mesh before changing it.

        // The next few lines create an array to store all vertices of all the scene objects that should react to the light.
		Vector3[] objverts = sceneObjects[0].gameObject.GetComponent<MeshFilter>().mesh.vertices;
        for (int i = 1; i < sceneObjects.Length; i++)
        {
            objverts = ConcatArrays(objverts, sceneObjects[i].GetComponent<MeshFilter>().mesh.vertices);
           
        }
        
        //these lines (1) an array of structs which will be used to populate the light mesh and (2) the vertices and UVs to ultimately populate the mesh.
        // (the "*2" is because there are twice as many rays casted as vertices, and the "+1" because the first point in the mesh should be the center of the light source)
        angledVerts[] angleds = new angledVerts[(objverts.Length*2)];
		Vector3[] verts = new Vector3[(objverts.Length*2)+1];
        Vector2[] uvs = new Vector2[(objverts.Length*2)+1];


        //Store the vertex location and UV of the center of the light source in the first locations of verts and uvs.
		verts[0] = lightRays.transform.worldToLocalMatrix.MultiplyPoint3x4(this.transform.position);
		uvs[0] = new Vector2(lightRays.transform.worldToLocalMatrix.MultiplyPoint3x4(this.transform.position).x, lightRays.transform.worldToLocalMatrix.MultiplyPoint3x4(this.transform.position).y);

        int h = 0; //a constantly increasing int to use to calculate the current location in the angleds struct array.

        for (int j = 0; j < sceneObjects.Length; j++) //cycle through all scene objects.
        {
            for (int i = 0; i < sceneObjects[j].GetComponent<MeshFilter>().mesh.vertices.Length; i++) //cycle through all vertices in the current scene object.
		    {
                Vector3 me = this.transform.position;// just to make the current position shorter to reference.
                Vector3 other = sceneObjects[j].transform.localToWorldMatrix.MultiplyPoint3x4(objverts[h]); //get the vertex location in world space coordinates.

                float angle1 = Mathf.Atan2(((other.y-me.y)-offset),((other.x-me.x)-offset));// calculate the angle of the two offsets, to be stored in the structs.
                float angle3 = Mathf.Atan2(((other.y-me.y)+offset),((other.x-me.x)+offset));
                
                RaycastHit hit; //create and fire the two rays from the center of the light source in the direction of the vertex, with offsets.
                Physics.Raycast(transform.position, new Vector2( (other.x-me.x)-offset , (other.y-me.y)-offset ) , out hit, 100, ~ignoreMe);
                RaycastHit hit2;
                Physics.Raycast(transform.position, new Vector2( (other.x-me.x)+offset , (other.y-me.y)+offset ), out hit2, 100, ~ignoreMe);

                //store the hit locations as vertices in the struct, in model coordinates, as well as the angle of the ray cast and the UV at the vertex.
                angleds[(h*2)].vert = lightRays.transform.worldToLocalMatrix.MultiplyPoint3x4(hit.point);
                angleds[(h*2)].angle = angle1;
                angleds[(h*2)].uv = new Vector2(angleds[(h*2)].vert.x, angleds[(h*2)].vert.y);

			    angleds[(h*2)+1].vert = lightRays.transform.worldToLocalMatrix.MultiplyPoint3x4(hit2.point);
                angleds[(h*2)+1].angle = angle3;
                angleds[(h*2)+1].uv = new Vector2(angleds[(h*2)+1].vert.x, angleds[(h*2)+1].vert.y);

                h++;//increment h.

                if(showRed && hit.collider != null)//for debugging: draw the rays cast.
                {
                    Debug.DrawLine(transform.position, hit.point, Color.red);		
                }
                if(showGreen)
                {
                    Debug.DrawLine(transform.position, hit2.point, Color.green);		
                }	

		    }
        }
        
        Array.Sort(angleds, delegate(angledVerts one, angledVerts two) {
                    return one.angle.CompareTo(two.angle);
                  });//sort the struct array of vertices from smallest angle to greatest.

        for (int i = 0; i < angleds.Length; i++)//store the values in the struct array in verts and uvs. 
        {                                       //(offsetting one because index 0 is the center of the light source and triangle fan)
            verts[i+1] = angleds[i].vert;
            uvs[i+1] = angleds[i].uv;
        }

		mesh.vertices = verts; //update the actual mesh with the new vertices.

        for (int i = 0; i < uvs.Length; i++)//offset all the UVs by .5 on both s and t to make the texture center be at the object center.
        {
            uvs[i] = new Vector2 (uvs[i].x + .5f, uvs[i].y + .5f);
        }

        mesh.uv = uvs; //update the actual mesh with the new UVs.
        
		int[] triangles = {0,1,verts.Length-1}; //init the triangles array, starting with the last triangle to orient normals properly.

		for (int i = verts.Length-1; i > 0; i--) //add all triangles to the triangle array, determined by three verts in the vertex array.
		{
			triangles = AddItemsToArray(triangles, 0, i, i-1);
		}
        //triangles = AddItemsToArray(triangles, 0, 1, 2);

		mesh.triangles = triangles; //update the actual mesh with the new triangles.
  	}


    void GetWalls()
    {
        
       
        sceneObjects = Physics.OverlapSphere(transform.position, getRadius, wallMask);
        
        
    }
}

MaskShader.shader

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Unlit/ScreenspaceTexture"
{
Properties
{
    _MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
    Tags { "RenderType"="Opaque" "Queue"="Geometry"}
    LOD 100

    Lighting Off

    Pass
    {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        // make fog work
        #pragma multi_compile_fog

        #include "UnityCG.cginc"

        struct appdata
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
        };

        struct v2f
        {
            float3 screenPos : TEXCOORD0;
            UNITY_FOG_COORDS(1)
            float4 vertex : SV_POSITION;
        };

        sampler2D _MainTex;
        float4 _MainTex_ST;

        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.screenPos = o.vertex.xyw;

            // This might be platform-specific. Test with OpenGL.
            o.screenPos.y *= -1.0f;

            UNITY_TRANSFER_FOG(o,o.vertex);
            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            // sample the texture
            float2 uv = (i.screenPos.xy / i.screenPos.z) * 0.5f + 0.5f;

			uv.y = 1.0 - uv.y;
            fixed4 col = tex2D(_MainTex, uv);

            // apply fog
            UNITY_APPLY_FOG(i.fogCoord, col);               
            return col;
        }
        ENDCG
    }
}
}