So i’m working on this project and i had a lot of fun working on it on single player but i felt that it would be good to make it multiplayer i have no experience with multiplayer so i used “Photon” ,
- first is this a close imagine with what i have
- then we get this you join the server and you see the person who plays for a while like a some just loged in i want people to join anytime and leave anytime
-so i have the codes here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerItems : Photon.MonoBehaviour {
public float hs;
public float Health;
public int RockAmouts;
public int ActiveRocks;
public Text Rocksa;
[PunRPC]
public void SetMyRocks(int Number){
RockAmouts += Number;
Debug.Log("player: "+RockAmouts);
Rocksa.text = "" + RockAmouts;
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if (stream.isWriting) {
//we own it
stream.SendNext (RockAmouts);
} else {
ActiveRocks = (int)stream.ReceiveNext();
}
}
}
- And i set the points here `
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ItemMine : Photon.MonoBehaviour {
public PlayerItems Pt;
public FindLocal fl;
public bool destroya;
public Sprite[] Sprites;
public void OnTriggerEnter2D(Collider2D other)
{
if (fl.localPlayer) {
PhotonView photonView = fl.localPlayer.GetComponent<PhotonView> ();
if (other.tag == "Player") {
fl.Itema = fl.localPlayer.GetComponent<PlayerItems> ();
photonView.RPC ("SetMyRocks", PhotonTargets., 1); // Call to RPC function
if (destroya == true) {
PhotonNetwork.Destroy (gameObject);
}
}
}
}
void OnMouseDown (){
Debug.Log("V IS BEING PRESSED!!");
// int projectileAttackRandomizer = Random.Range (1,Sprites.Length);
GameObject rocks = PhotonNetwork.Instantiate("RockPoint", this.transform.position, this.transform.rotation, 0);
// rocks.GetComponent().sprite = Sprites[projectileAttackRandomizer -= 1];
}
}
`
- and Finally finding my local player `
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FindLocal : Photon.MonoBehaviour {
public GameObject localPlayer;
public Transform Caamera;
public PlayerItems Itema;
public void Update() {
if (localPlayer == null) {
foreach (GameObject cur in GameObject.FindGameObjectsWithTag("Player")) {
Debug.Log (cur);
Debug.Log ("Iterating");
if (cur.GetComponent<PhotonView> ().photonView.isMine == true) {
Debug.Log ("Found local player");
localPlayer = cur;
}
}
}
if (localPlayer == null) {
// Debug.Log ("Couldn't find local player");
}
if (localPlayer != null) {
// localPlayer.Itema
if(Caamera == null){
Caamera = Camera.main.transform;
}
if (Itema == null) {
Itema = localPlayer.GetComponent<PlayerItems> ();
}
}
}
}`