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
    }
}
}
Posted on

How To Make Among Us – Reactor Task/Mini-Game

Let’s show you how to make Among Us – reactor task/mini-game! We are showing how to make player movement and eventually multiplayer for Among Us with the series located here: https://www.infogamerhub.com/among-us-lesson-1/

This series is focused on the task/mini-game portion of Among Us. The first task/mini-game we are going to make in this series is the Reactor task shown in the image below.

How To Make Among Us - Reactor Task/Mini-Game

If you want to learn how to create this task then follow along in Unity while watching the video below. If you do follow along and aren’t able to create the task with the video alone we recommend becoming a member for just a few dollars a month gives you access to the scripts, screenshots, and many other perks that will further assist you in making this task. I will also be showing only members how to add features to this task to make it more challenging and difficult.

Attention Members:

The second half of this post will show how to make the game more difficult and it is for members only. We hope if you are not a member you will try us out for the extra support and content. The second part of this post for members is coming soon!

How To Make Among Us – Reactor Task/Mini-Game Video:

Hierarchy:

This content is restricted to members only.

How To Make Among Us - Reactor Task/Mini-Game
This games hierarchy with notes.
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
}

Inspectors:

This content is restricted to members only.

You can zoom in by right clicking and opening the image in a new tab.

How To Make Among Us - Reactor Task/Mini-Game
Most of the important inspector views for the game. Look at the top of each column to pair it with the game object in the hierarchy.
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
}

The Reactor Task/Mini-Game Script:

This content is restricted to members only.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NumberOrderMiniGame : MonoBehaviour
{
    [SerializeField] int nextButton;
    [SerializeField] GameObject GamePanel;
    [SerializeField] GameObject[] myObjects;

    int randNum;
    //Use this to change the hierarchy of the GameObject siblings
    void Start()
    {
        nextButton = 0;
    }
    private void OnEnable()  //Gets called when the game object this script is attached to becomes active
    {
        nextButton = 0;
        for (int i = 0; i < myObjects.Length; i++)
        {
            myObjects[i].transform.SetSiblingIndex(Random.Range(0, 9)); 
            //transform.SetSiblingIndex(Random.Range(0, 9)); 
        }
    }   
   
    public void ButtonOrder(int button)
    {
        Debug.Log("Pressed");
        if (button == nextButton)
        {
            nextButton++;
            Debug.Log("Next Button" + nextButton);
        }
        else
        {
            Debug.Log("Failed");
            Debug.Log("Next Button" + nextButton);
            nextButton = 0;
            OnEnable();
        }
        if (button == 9 && nextButton == 10)
        {
            Debug.Log("Pass");
            nextButton = 0;
            ButtonOrderPanelClose();
        }
    }
    public void ButtonOrderPanelClose() //Set this function to the close button On click in the inspector
    {
        GamePanel.SetActive(false);
    }
    public void ButtonOrderPanelOpen() //Set this function to the Play or Open button On click in the inspector
    {
        GamePanel.SetActive(true);
    }
}
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
}

Let Us Know How We Did

We are wanting to always make our content more helpful and we can’t do it without your input. Please leave us a comment on this posts YouTube video here: https://youtu.be/SPPuculq7-8 let us know how we did. If there are other ideas on how to use the code or better ways to implement the code in this video also let us know in the YouTube comment section.

Posted on

Among Us in Unity – Kills and Bodies (Lesson 6)

Player Presidential Slap Android iOS

In this lesson on how to create Among Us in Unity, we will continue on with our kill mechanic and make it so a body object is left behind after a player is killed. To do this we will need to first create the dead body prefab. The dead body prefab is an empty game object with a rigid body and sphere collider attached to it. We then have two sprite objects as children to the empty object. We then need to apply the body sprite to these sprite renderer objects.

Once you have these objects created we will then need to create a new script and add it to the body object. This script only has one variable and function which are used for changing the color of this object’s sprite.

We then need to go to our player controller script and change some of the code from our previous video. We will also add code to instantiate the body prefab at the position of our player when our player is killed.

Unlock Code and Member Content

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;
    }
}

AU_PlayerController.cs

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
    static 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;

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

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

    private void OnDisable()
    {
        WASD.Disable();
        KILL.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;
    }

    // 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);
        }

    }

    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);
            }
        }
    }

    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()
    {
        isDead = true;

        myAnim.SetBool("IsDead", isDead);
        myCollider.enabled = false;

        AU_Body tempBody = Instantiate(bodyPrefab, transform.position, transform.rotation).GetComponent<AU_Body>();
        tempBody.SetColor(myAvatarSprite.color);
    }

}
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
}