Photon game like minecraft
Hello everyone, I have a question, I am creating a minecraft-type game, the block generator is created in the game itself, and I need to synchronize this through the RPC, I tried it, but instead of it doing setactive true for all players, it is called for all players and set locally my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
namespace vGenerator
{
public class PhotonSerialize : MonoBehaviourPunCallbacks
{
//private int _vIndex;
public PhotonView pv;
public Chunk chunk;
public VoxelController vxC;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (pv.IsMine)
{
pv.RPC("Chunky", RpcTarget.All);
}
}
vxC = GameObject.Find("VoxelGenerator(Clone)").GetComponent<VoxelController>();
//chunk = GameObject.Find("(0, 0, 0)").GetComponent<Chunk>();
//parent = GameObject.Find("VoxelGenerator(Clone)");
//parent.transform.parent = child.transform;
}
[PunRPC]
public void Chunky()
{
vxC.SetVoxel();
}
}
}
and this 2 code
using UnityEngine;
using System;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
namespace vGenerator
{
public class VoxelController : MonoBehaviourPunCallbacks
{
public static Action SetVoxelEvent;
public int _vIndex;
public Camera _cam;
private RaycastHit _hit;
public Chunk _currentChunk;
private float _perSecond = 0;
#region Services
private IFactoryService _factoryService;
private IPaintableService _paintableService;
private IVoxelUtilService _vUtility;
private GameStateMachine _gameStateMachine;
#endregion
internal void Init(
IVoxelUtilService vUtility,
Chunk currentChunk,
GameStateMachine gameStateMachine)
{
_vUtility = vUtility;
_currentChunk = currentChunk;
_factoryService = ServicesIterator.Container.Single<IFactoryService>();
_paintableService = ServicesIterator.Container.Single<IPaintableService>();
_gameStateMachine = gameStateMachine;
}
private void Update()
{
if (_currentChunk == null)
return;
if (!_gameStateMachine.GetEqualState<EditingState>())
return;
Ray ray = GetRay();
if (Physics.Raycast(ray, out _hit, 100f, 1 << LayerMask.NameToLayer("Voxel")))
{
Vector3 woldPosition = _hit.point - ray.direction * 0.01f;
_vIndex = GetVoxelIndex(woldPosition);
if (Input.GetMouseButtonDown(0))
{
SetVoxel();
_perSecond = 0;
}
if (Input.GetMouseButton(0))
{
_perSecond += Time.deltaTime;
if (_perSecond >= 0.2f)
{
ConstantlySetVoxel();
_perSecond = 0;
}
return;
}
if (Input.GetMouseButtonDown(1) && _hit.transform.CompareTag("Voxel"))
DisableVoxel(_hit.transform);
EnableFakeVoxel();
}
else
DisableFakeVoxel();
}
[PunRPC]
public void SetVoxel()
{
_currentChunk.EnableVoxel(_vIndex);
SetVoxelEvent.Invoke();
}
[PunRPC]
public void DisableVoxel(Transform t)
=> _currentChunk.DisableVoxel(t);
public void ChangeFakeVoxelColor()
=> _currentChunk.ChangeFakeVoxelColor();
private void EnableFakeVoxel()
=> _currentChunk.ShowFakeVoxel(_vIndex, _hit);
private void DisableFakeVoxel()
=> _currentChunk.DisableFakeVoxel();
[PunRPC]
private void ConstantlySetVoxel()
{
// int vIndex = GetConstantlyVoxelIndex(worldPosition);
_currentChunk.EnableVoxelConstantly(_vIndex);
SetVoxelEvent.Invoke();
}
private int GetVoxelIndex(Vector3 worldPosition)
{
Vector3Int chunkPosition = _vUtility.WorldToChunk(worldPosition, _currentChunk._chunkSize);
Vector3Int gridPosition = _vUtility.WorldToGrid(worldPosition, chunkPosition, _currentChunk._chunkSize);
return _vUtility.To1DIndex(gridPosition, _currentChunk._chunkSize);
}
private Ray GetRay()
=> Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
}
}
thx to all help