Hello everyone. I’m trying to make a multiplayer tower defense game which each player can spawn towers as long as their have money. Everything’s working fine except for one thing: the second player can’t use his/her camera. I’m using the default NetworkManager to spawn players which are the cameras. Each camera has a NetworkIdentity with Local Player Authority check and NetworkTransform. Same thing with the cube i’m trying to spawn. The camera uses raycast to click on stuff and get position.
So I’m using a [Command] function to manipulate the raycast and the spawn. Looks like only the host can move and spawn objects because the client can’t enter this Cmd function. Then, I tried to use [ClientRpc], however it seems that clients can’t call this method. I now find myself lost and don’t know what to do. How the client is going to send information to the sever? How’s the game could know where’s he or she is pointing at and be able to spawn an object for both players?
Any suggestions?
Thanks in advance. Here’s the code:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class SpawnObjectOnClick : NetworkBehaviour {
Ray myRay;
RaycastHit hit;
public GameObject cube;
void Update(){
if (!isLocalPlayer) {
return;
}
CmdGetPosition ();
}
[Command]
void CmdGetPosition(){
myRay = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (myRay, out hit)) {
if (Input.GetMouseButtonDown (0)) {
GameObject newObject = (GameObject)Instantiate (cube, hit.point, Quaternion.Euler (0, 90, 0));
NetworkServer.Spawn(newObject);
}
}
}
}
Hi guys, found a solution!
using UnityEngine;
using System;
using System.Collections;
using UnityEngine.Networking;
public class SetTowerOnline : NetworkBehaviour
{
public int selected;
public GameObject towers;
public float[] prices;
public GameObject tile;
public Vector3 PositionGetter;
public Vector3 pos;
public GameObject bulletPrefab;
public Transform bulletSpawn;
private Money moneyScript;
Camera mainCamera;
void Start ()
{
mainCamera = GetComponent<Camera>();
this.moneyScript = GameObject.Find ("GameLogic").GetComponent<Money> ();
}
void Spawn(){
Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
Debug.Log ("Raycast disparado!");
if (Physics.Raycast (ray, out hit, 20)) {
if (hit.transform.tag == "tile") {
this.tile = hit.transform.gameObject;
Debug.Log ("Estou no tile");
} else {
this.tile = null;
Debug.Log ("Estou fora do tile");
}
}
if (Input.GetMouseButtonDown (0) && this.tile != null) {
TileTaken tileTakenOnlineScript = this.tile.GetComponent<TileTaken> ();
if (!tileTakenOnlineScript.isTaken && (this.moneyScript.money - prices [this.selected]) >= 0) {
this.moneyScript.money -= prices [this.selected];
pos = new Vector3 (this.tile.transform.position.x, 1.1f, this.tile.transform.position.z);
PositionGetter = pos;
GameObject newObject = (GameObject)Instantiate (towers, transform.position, Quaternion.Euler (0, 90, 0));
newObject.transform.position = pos;
CmdClientSpawn ();
if (selected != 3) {
tileTakenOnlineScript.isTaken = true;
}
}
}
}
[Command]
void CmdClientSpawn(){
GameObject newObject = (GameObject)Instantiate (towers, transform.position, Quaternion.Euler (0, 90, 0));
newObject.transform.position = PositionGetter;
NetworkServer.Spawn (newObject);
}
void Update ()
{
if (!isLocalPlayer)
{
return;
}
Spawn ();
}
}
Commands send information from client to server. So, apparently, you should not deal with the client raycast on the host. The only thing you should do is to send a instantiate position and what you want to instantiate to the server. I was thinking that the client wasn’t getting to the Cmd function, but it was, only I coundn’t see, not from client pespective anyway. The only problem now is that the host is not receiving the PositionGetter value and is spawning objects on default position. For this, guess I going to create a function for each possible position, since is not that many.
If anyone’s interested, this is the tutorial for tower defence I following:
And for multiplayer:
Both are very good tutorials.