using System;
using UnityEngine;
using Cinemachine;
using Photon.Pun;
using Photon.Realtime;
using ExitGames.Client.Photon;
public class PlayerController : MonoBehaviourPunCallbacks
{
private Animator _playerAnimator;
public PhotonView PV;
[SerializeField] private bool isDancing;
RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.All };
public void Emote()
{
// Checks if player isDancing and if player isn't dancing run the Emote code if player code is dancing then cancel Emote
if (!isDancing)
{
isDancing = true;
// Swaps camera to the 3rd person camera
emoteCamera.Priority = 2;
// Checks if Emote PlayerPref is set
if (PlayerPrefs.HasKey("Emote"))
{
// Save Emote PlayerPref to string variable
string emote = PlayerPrefs.GetString("Emote");
// Use Switch statement to check the Emote string
switch (emote)
{
case "Pushup":
// Raise an event through PhotonNetwork using the EmoteAnimate event code containing the player PhotonView ID and emote string
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "Loser":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "Threatening":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "ChickenDance":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "DancingMaraschinoStep":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "DancingTwerk":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "GangnamStyle":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "MacarenaDance":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "RobotHipHopDance":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "Shuffling":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "SillyDancing":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
case "TwistDance":
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, emote }, raiseEventOptions, SendOptions.SendReliable);
break;
}
}
// If no PlayerPref is saved set PlayerPref to default "Loser" and raise an event
else
{
PlayerPrefs.SetString("Emote", "Loser");
PhotonNetwork.RaiseEvent(EmoteAnimate, new object[] { PV.ViewID, "Loser" }, raiseEventOptions, SendOptions.SendReliable);
}
} else
{
PhotonNetwork.RaiseEvent(CancelEmoteAnimate, new object[] { PV.ViewID }, raiseEventOptions, SendOptions.SendReliable);
}
}
// Called when animation is cancelled
private void CancelEmote()
{
isDancing = false;
// Swaps camera back to the FPS camera
emoteCamera.Priority = 0;
_playerAnimator.SetTrigger("CancelEmote");
}
// Plays animations
private void Animations()
{
// Sets animation inputs
_playerAnimator.SetFloat("InputX", x);
_playerAnimator.SetFloat("InputY", y);
// Sets bool for isCrouching
_playerAnimator.SetBool("isCrouching", isCrouching);
// Sets bool for isGrounded
_playerAnimator.SetBool("isGrounded", isGrounded);
// Sets bool for isJump
_playerAnimator.SetBool("isJump", isJumping);
// Sets bool for isWallrunning
_playerAnimator.SetBool("isWallrunning", onWall);
}
public const byte EmoteAnimate = 1;
public const byte CancelEmoteAnimate = 2;
// Raised events through photon are recieved here
private void OnEvent(EventData photonEvent)
{
// Saves the EventCode
byte eventCode = photonEvent.Code;
// If event code is the same as EmoteAnimate run code
if (eventCode == EmoteAnimate)
{
// Saves PhotonEvent data to an object
object[] data = (object[])photonEvent.CustomData;
// Gets the PV from data variable
int targetPV = (int)data[0];
// If target is the same as PV ID then run animation sent through the event
if (targetPV == PV.ViewID)
{
_playerAnimator.ResetTrigger("CancelEmote");
Debug.Log((string)data[1]);
_playerAnimator.SetTrigger((string)data[1]);
}
} else if (eventCode == CancelEmoteAnimate)
{
object[] data = (object[])photonEvent.CustomData;
int targetPV = (int)data[0];
if (targetPV == PV.ViewID)
{
CancelEmote();
}
}
}
public override void OnEnable()
{
base.OnEnable();
// Subscribes the EventsReceived to the OnEvent function
PhotonNetwork.NetworkingClient.EventReceived += OnEvent;
}
public override void OnDisable()
{
base.OnDisable();
// Unsubscribes the EventsReceived to the OnEvent function
PhotonNetwork.NetworkingClient.EventReceived -= OnEvent;
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter