TheKingsRace/Assets/Scripts/KingScripts/Goo.cs
Julia Butenhoff 99fe74a8d9 Fixed King Null Ref Errors
- Fixed KingPlace and KingMove Null Refs
- Changed the Networked Slime to isTrigger instead of solid
2022-03-30 13:27:01 -05:00

81 lines
2.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAPI;
using MLAPI.Messaging;
public class Goo : MonoBehaviour
{
int Lifetime = 0;
void FixedUpdate() {
Lifetime++;
if(Lifetime == 850) {
if (NetworkManager.Singleton.IsServer) {
DespawnMyselfServerRPC();
}
}
}
void OnTriggerEnter(Collider other) {
if (other.transform.gameObject.tag == "PlayerTrigger" && other.gameObject.transform.root.gameObject.GetComponent<NetworkObject>().OwnerClientId == NetworkManager.Singleton.LocalClientId) {
ApplyGooServerRPC(other.gameObject.transform.root.gameObject.GetComponent<NetworkObject>().OwnerClientId);
}
}
[ServerRpc(RequireOwnership = false)]
private void ApplyGooServerRPC(ulong playerID) {
ClientRpcParams clientRpcParams = new ClientRpcParams {
Send = new ClientRpcSendParams {
TargetClientIds = new ulong[] { playerID }
}
};
ApplyGooClientRPC(playerID, clientRpcParams);
}
[ClientRpc]
private void ApplyGooClientRPC(ulong playerID, ClientRpcParams clientRpcParams) {
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject player in players) {
if (player.GetComponent<NetworkObject>().OwnerClientId == playerID) {
player.GetComponentInChildren<PlayerStats>().ApplySlimeTrail();
}
}
}
void OnTriggerExit(Collider other) {
if (other.transform.gameObject.tag == "PlayerTrigger" && other.gameObject.transform.root.gameObject.GetComponent<NetworkObject>().OwnerClientId == NetworkManager.Singleton.LocalClientId) {
RemoveGooServerRPC(other.gameObject.transform.root.gameObject.GetComponent<NetworkObject>().OwnerClientId);
}
}
[ServerRpc(RequireOwnership = false)]
private void RemoveGooServerRPC(ulong playerID) {
ClientRpcParams clientRpcParams = new ClientRpcParams {
Send = new ClientRpcSendParams {
TargetClientIds = new ulong[] { playerID }
}
};
RemoveGooClientRPC(playerID, clientRpcParams);
}
[ClientRpc]
private void RemoveGooClientRPC(ulong playerID, ClientRpcParams clientRpcParams) {
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject player in players) {
if (player.GetComponent<NetworkObject>().OwnerClientId == playerID) {
player.GetComponentInChildren<PlayerStats>().ClearSlimeTrail();
}
}
}
[ServerRpc(RequireOwnership = false)]
private void DespawnMyselfServerRPC() {
this.gameObject.GetComponent<NetworkObject>().Despawn(true);
}
}