So what I have been trying to achieve is to call an rpc and have it create 100 trees across the map and I have achieved that but for every player the trees are in different places. . . so here's the code:
using UnityEngine;
using System.Collections;
public class SpawnIsland : MonoBehaviour {
public GameObject[] Islands;
private GameObject GameIsland;
public int IslandNumber;
public GameObject[] Resources;
int Tree = 100;
void Start () {
IslandNumber = Random.Range (0, 2);
}
void Update(){
if(Tree > 0){
networkView.RPC ("SpawnResources",RPCMode.AllBuffered);
}
}
[RPC]
public void SpawnResources(){
if (Tree >= 1) {
RaycastHit hit;
float x = RandomX ();
float z = RandomZ ();
if (Physics.Raycast (new Vector3 (transform.position.x + x, 1500, transform.position.z + z), -Vector3.up, out hit)) {
float distanceToGround = hit.distance;
if(hit.collider.tag == "Island"){
GameObject TreeObject = (GameObject)Instantiate (Resources[0],new Vector3(transform.position.x + x,1500-distanceToGround,transform.position.z + z),transform.rotation);
TreeObject.name = "Tree" + " " + Tree;
Debug.Log(gameObject.transform.name + " " + "spawing" + " " + TreeObject);
TreeObject.transform.parent = transform;
Tree--;
}
}
}
}
float RandomX(){
int x;
x = Random.Range (-5000,5000);
return x;
}
float RandomZ(){
int z;
z = Random.Range (-5000,5000);
return z;
}
}
Also this code is only enabled on the host's client. Now when I was making the script I thought that if I put the co ordinate generator in the rpc it would then make it different for every client so that is why I made the two external functions (RandomX and RandomZ) but that as not done the job. So if someone could tweak this script so that the trees (called resources) would spawn at the same place for every client then that would be great! Thanks.
↧