Dear all,
I try to understand the network principles for multi-user environments.
I spent time in the great “Ultimate Unity Networking Project by M2H” tutorial. Great thanks to Leepo !!!
But I’m still confused about how to construct the RPC calls.
I have success with some simple example found in the resources like that Java script…
var cubePrefab : Transform;
function OnGUI () {
if (GUI.Button (Rect (45,150,100,30),"SpawnBox"))
{
var viewID : NetworkViewID= Network.AllocateViewID();
networkView.RPC("SpawnBox",RPCMode.AllBuffered,viewID,transform.position);
}
}
@RPC
function SpawnBox (viewID : NetworkViewID, location : Vector3) {
// Instantate the prefab locally
var clone : Transform;
clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform;
var nView : NetworkView;
nView = clone.GetComponent(NetworkView);
nView.viewID = viewID;
}
But I’m pretty lost when I try to adapt that C# script
using UnityEngine;
using System.Collections;
public class MouseInteraction : MonoBehaviour {
GameObject minePrefab;
// Use this for initialization
void Start () {
minePrefab = Resources.Load("minePrefab") as GameObject;
}
// Update is called once per frame
void Update () {
Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if(Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(ray,out hit))
{
//We've hit something
if(hit.collider.gameObject.name.Equals("ground"))
{
//We've hit the ground
CreateMine(hit.point);
}
}
} else if(Input.GetMouseButtonDown(1)) {
if(Physics.Raycast(ray,out hit))
{
//We've hit something
if(hit.collider.gameObject.name.Equals("minePrefab(Clone)"))
{
//We've hit a mine
GameObject.Destroy(hit.collider.gameObject);
networkView.RPC("CreateMine", RPCMode.All);
}
}
}
}
[RPC]
void CreateMine(Vector3 pos)
{
Instantiate(minePrefab, pos, Quaternion.identity);
}
}
Basically, that script allow to create instanced prefab on mouse position, left click, or to delete the object on mouse right click.
I’d like that to be visible and available to all users (clients or server).
I’ll continue to search but if one of you around could advise me and help to understand how to construct it in C#, it will be simply great.
Thank you all,
Pascal