RPC to change local player color and hats, tried but cant get it to work, working with local player

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;


public class AU_ColorCustomizer : MonoBehaviour
{


[SerializeField] Color[] allColors;

[SerializeField] Sprite[] allHats;
[SerializeField] GameObject colorPanel;
[SerializeField] GameObject hatPanel;
[SerializeField] Button colorTabButton;
[SerializeField] Button hatTabButton;





public void SetColor(int colorIndex)
{
AU_PlayerController.localPlayer.SetColor(allColors[colorIndex]);

}


public void SetHat(int hatIndex)
{
AU_PlayerController.localPlayer.SetHat(allHats[hatIndex]);
}

public void NextScene(int sceneIndex)
{
SceneManager.LoadScene(sceneIndex);

}

public void EnableColors()
{
colorPanel.SetActive(true);
hatPanel.SetActive(false);
colorTabButton.interactable = false;
hatTabButton.interactable = true;
}

public void EnableHats()
{
colorPanel.SetActive(false);
hatPanel.SetActive(true);
colorTabButton.interactable = true;
hatTabButton.interactable = false;
}


}




using System.Collections.Generic;
using Photon.Pun;
using UnityEngine;
using UnityEngine.InputSystem;
using System.IO;
public class AU_PlayerController : MonoBehaviour,IPunObservable
{

public static AU_PlayerController localPlayer;


Rigidbody myRB;
Transform myAvatar;
Vector2 movementInput;
[SerializeField] InputAction WASD;
[SerializeField] float movementSpeed;
Animator myAnim;


static Color myColor;
SpriteRenderer myAvatarSprite;


static Sprite myHatSprite;
SpriteRenderer myHatHolder;


[SerializeField] bool isImposter;
[SerializeField] InputAction KILL;


[SerializeField] Collider myCollider;


bool isDead;

List<AU_PlayerController> targets;


[SerializeField] GameObject bodyPrefab;

public static List<Transform> allBodies;
List<Transform> bodiesFound;


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


[SerializeField] InputAction MOUSE;
Vector2 mousePositionInput;
Camera myCamera;


[SerializeField] InputAction INTERACTION;

[SerializeField] LayerMask interactLayer;


//Photon Variables
[SerializeField] GameObject lightMask;
PhotonView myPV;
float direction = 1;


private void Awake()
{

INTERACTION.performed += Interact;
KILL.performed += KillTarget;
REPORT.performed += ReportBody;
}


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

}



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

}

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


myPV = GetComponent<PhotonView>();



if (myPV.IsMine)
{


localPlayer = this;
}



myCamera = transform.GetChild(1).GetComponent<Camera>();
targets = new List<AU_PlayerController>();
myRB = GetComponent<Rigidbody>();
myAvatar = transform.GetChild(0);
myAnim = GetComponent<Animator>();


myAvatarSprite = myAvatar.GetComponent<SpriteRenderer>();

myHatHolder = myAvatar.GetChild(1).GetComponent<SpriteRenderer>();



if (!myPV.IsMine)
{
myCamera.gameObject.SetActive(false);
lightMask.SetActive(false);
return;
}



if (myColor == Color.clear)
myColor = Color.white;



myAvatarSprite.color = myColor;

allBodies = new List<Transform>();
bodiesFound = new List<Transform>();


if (myHatSprite != null)
myHatHolder.sprite = myHatSprite;
}

// Update is called once per frame
void Update()
{
myAvatar.localScale = new Vector2(direction, 1);
if (!myPV.IsMine)
return;
movementInput = WASD.ReadValue<Vector2>();
myAnim.SetFloat("Speed", movementInput.magnitude);
if (movementInput.x != 0)
{
direction = Mathf.Sign(movementInput.x);

}

if(allBodies.Count > 0)
{
BodySearch();
}
mousePositionInput = MOUSE.ReadValue<Vector2>();



}


private void FixedUpdate()
{
if (!myPV.IsMine)
return;
myRB.velocity = movementInput * movementSpeed;
}





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


public void SetHat(Sprite newHat)
{

myHatSprite = newHat;
myHatHolder.sprite = myHatSprite;
}



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 (!myPV.IsMine)
return;
if (!isImposter)
return;


if(context.phase == InputActionPhase.Performed)
{
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[targets.Count - 1].myPV.RPC("RPC_KILL",RpcTarget.All);

targets.RemoveAt(targets.Count - 1);

}
}
}

[PunRPC]

void RPC_KILL()
{
Die();
}








public void Die()
{
if (!myPV.IsMine)
return;
AU_Body tempBody = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "AU_Body"), transform.position, transform.rotation).GetComponent<AU_Body>();
tempBody.SetColor(myAvatarSprite.color);

isDead = true;
myAnim.SetBool("isDead", isDead);
gameObject.layer = 8;
myCollider.enabled = false;



}

private void BodySearch()
{
foreach(Transform body in allBodies)
{
RaycastHit hit;
Ray ray = new Ray(transform.position, body.position - transform.position);

if (Physics.Raycast(ray, out hit, 1000f, ~ignoreForBody))
{
if(hit.transform == body)
{
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();
}



void Interact (InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Performed)
{

RaycastHit hit;
Ray ray = myCamera.ScreenPointToRay(mousePositionInput);
if(Physics.Raycast(ray, out hit, interactLayer))
{
if (hit.transform.tag == "Interactable")
{
if (!hit.transform.GetChild(0).gameObject.activeInHierarchy)
return;
AU_Interactable temp = hit.transform.GetComponent<AU_Interactable>();
temp.PlayMiniGame();


}
if (hit.transform.tag == "Vent")
{
myAnim.SetBool("Vent", true);
AU_Interactable temp = hit.transform.GetComponent<AU_Interactable>();
temp.PlayMiniGame();

}

}



}
}



public void ExitVent()
{
myAnim.SetBool("Vent", false);
}




public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(direction);
stream.SendNext(isImposter);
}
else
{
this.direction = (float)stream.ReceiveNext();
this.isImposter = (bool)stream.ReceiveNext();
}
}


public void BecomeImposter(int ImposterNumber)
{
if(PhotonNetwork.LocalPlayer == PhotonNetwork.PlayerList[ImposterNumber])
{
isImposter = true;
}
}







}

Comments

Sign In or Register to comment.