Building over network
Hey
im trying to make it so players can build structures during game play
the problem im having is wen one player builds a structure another copy of the structure spawns under another player
the script that controls the BUILDING GUN
[code2=csharp]using UnityEngine;
using System.Collections;
/// <summary>
/// this script is attached to a weapon
/// and allows the player to use said weapon
/// </summary>
public class BuildingGun : MonoBehaviour {
//Variables Start________________________
public Transform gunmount;
private Transform mytransform;
private Transform bulletexit;
private Vector3 launchposition = new Vector3();
public bool Building = false;
bool deciding = false;
private float cooldown = 0;
public float range = 20;
public RaycastHit hit;
string buildorder = "BuildingObject";
//Variables End________________________
// Use this for initialization
void Start ()
{
mytransform = transform;
bulletexit = mytransform.FindChild ("BulletHole");
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.Alpha1)) {
GameObject gun2 = (GameObject)PhotonNetwork.Instantiate("StartingWeapon", mytransform.position,mytransform.rotation,0);
gun2.transform.parent = this.transform.parent;
((MonoBehaviour)gun2.GetComponent("StartingGun")).enabled = true;
PhotonNetwork.Destroy(gameObject);
}
if(Input.GetButtonDown("Fire1") && Building == false)
{
Building = true;
}
if (Building == true)
{
RayCastBuilding();
}
if (Input.GetButtonDown ("Fire2") && Building == false)
{
RayCastDestroy();
}
}
void RayCastBuilding ()
{
//Debug.DrawRay (bulletexit.position, bulletexit.forward, Color.red, range);
if (Physics.Raycast (bulletexit.position, bulletexit.forward, out hit, range))
{
if (hit.collider != null)
{
GameObject Marker = (GameObject)PhotonNetwork.Instantiate ("BuildingMarker", hit.point, Quaternion.identity, 0);
((MonoBehaviour)Marker.GetComponent("BuildingMarker")).enabled = true;
deciding = true;
if(Input.GetButtonDown("Fire1") && Building == true)
{
if (PhotonNetwork.offlineMode == true)
{
GameObject BuildingOffline = (GameObject)PhotonNetwork.Instantiate (buildorder, hit.point, Quaternion.identity, 0);
((MonoBehaviour)BuildingOffline.GetComponent ("BuildingScript")).enabled = true;
}else{
GetComponent<PhotonView> ().RPC ("BuildStructure", PhotonTargets.AllBufferedViaServer, buildorder);
}
Building = false;
deciding = false;
}
}else {
deciding = false;
}
}
}
void RayCastDestroy ()
{
if (Physics.Raycast (bulletexit.position, bulletexit.forward, out hit, range))
{
if (hit.collider != null)
{
PhotonNetwork.Destroy(hit.collider.transform.parent.gameObject);
}
}
}
[RPC]
public void BuildStructure(string build)
{
GameObject Building = (GameObject)PhotonNetwork.Instantiate (build, hit.point, Quaternion.identity, 0);
((MonoBehaviour)Building.GetComponent ("BuildingScript")).enabled = true;
}
}[/code2]
the script that controls the marker for where the vbuilding will be placed
the problem im having with this one is that other players can see the marker
[code2=csharp]using UnityEngine;
using System.Collections;
public class BuildingMarker : MonoBehaviour {
float destroytime = 0.001f;
// Use this for initialization
void Start ()
{
StartCoroutine (DestroyMe ());
}
// Update is called once per frame
void Update ()
{
}
IEnumerator DestroyMe ()
{
yield return new WaitForSeconds (destroytime);
PhotonNetwork.Destroy (gameObject);
}
}[/code2]
now the script for the structure itself
this is the one that the player spawns
how ever it also spawns a copy under other players
[code2=csharp]using UnityEngine;
using System.Collections;
public class BuildingScript : MonoBehaviour {
float buildtime = 60;
Transform mytransform;
bool built = false;
BuildingComputer comp;
// Use this for initialization
void Start () {
mytransform = transform;
comp = mytransform.FindChild("BuildingComputer").GetComponent<BuildingComputer> ();
}
// Update is called once per frame
void Update () {
buildtime -= Time.deltaTime;
if (buildtime >= 0) {
built = true;
}
if (built) {
comp.isbuilt = true;
}
}
}[/code2]
if anyone can help me out here
i would really appreciate it
im trying to make it so players can build structures during game play
the problem im having is wen one player builds a structure another copy of the structure spawns under another player
the script that controls the BUILDING GUN
[code2=csharp]using UnityEngine;
using System.Collections;
/// <summary>
/// this script is attached to a weapon
/// and allows the player to use said weapon
/// </summary>
public class BuildingGun : MonoBehaviour {
//Variables Start________________________
public Transform gunmount;
private Transform mytransform;
private Transform bulletexit;
private Vector3 launchposition = new Vector3();
public bool Building = false;
bool deciding = false;
private float cooldown = 0;
public float range = 20;
public RaycastHit hit;
string buildorder = "BuildingObject";
//Variables End________________________
// Use this for initialization
void Start ()
{
mytransform = transform;
bulletexit = mytransform.FindChild ("BulletHole");
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.Alpha1)) {
GameObject gun2 = (GameObject)PhotonNetwork.Instantiate("StartingWeapon", mytransform.position,mytransform.rotation,0);
gun2.transform.parent = this.transform.parent;
((MonoBehaviour)gun2.GetComponent("StartingGun")).enabled = true;
PhotonNetwork.Destroy(gameObject);
}
if(Input.GetButtonDown("Fire1") && Building == false)
{
Building = true;
}
if (Building == true)
{
RayCastBuilding();
}
if (Input.GetButtonDown ("Fire2") && Building == false)
{
RayCastDestroy();
}
}
void RayCastBuilding ()
{
//Debug.DrawRay (bulletexit.position, bulletexit.forward, Color.red, range);
if (Physics.Raycast (bulletexit.position, bulletexit.forward, out hit, range))
{
if (hit.collider != null)
{
GameObject Marker = (GameObject)PhotonNetwork.Instantiate ("BuildingMarker", hit.point, Quaternion.identity, 0);
((MonoBehaviour)Marker.GetComponent("BuildingMarker")).enabled = true;
deciding = true;
if(Input.GetButtonDown("Fire1") && Building == true)
{
if (PhotonNetwork.offlineMode == true)
{
GameObject BuildingOffline = (GameObject)PhotonNetwork.Instantiate (buildorder, hit.point, Quaternion.identity, 0);
((MonoBehaviour)BuildingOffline.GetComponent ("BuildingScript")).enabled = true;
}else{
GetComponent<PhotonView> ().RPC ("BuildStructure", PhotonTargets.AllBufferedViaServer, buildorder);
}
Building = false;
deciding = false;
}
}else {
deciding = false;
}
}
}
void RayCastDestroy ()
{
if (Physics.Raycast (bulletexit.position, bulletexit.forward, out hit, range))
{
if (hit.collider != null)
{
PhotonNetwork.Destroy(hit.collider.transform.parent.gameObject);
}
}
}
[RPC]
public void BuildStructure(string build)
{
GameObject Building = (GameObject)PhotonNetwork.Instantiate (build, hit.point, Quaternion.identity, 0);
((MonoBehaviour)Building.GetComponent ("BuildingScript")).enabled = true;
}
}[/code2]
the script that controls the marker for where the vbuilding will be placed
the problem im having with this one is that other players can see the marker
[code2=csharp]using UnityEngine;
using System.Collections;
public class BuildingMarker : MonoBehaviour {
float destroytime = 0.001f;
// Use this for initialization
void Start ()
{
StartCoroutine (DestroyMe ());
}
// Update is called once per frame
void Update ()
{
}
IEnumerator DestroyMe ()
{
yield return new WaitForSeconds (destroytime);
PhotonNetwork.Destroy (gameObject);
}
}[/code2]
now the script for the structure itself
this is the one that the player spawns
how ever it also spawns a copy under other players
[code2=csharp]using UnityEngine;
using System.Collections;
public class BuildingScript : MonoBehaviour {
float buildtime = 60;
Transform mytransform;
bool built = false;
BuildingComputer comp;
// Use this for initialization
void Start () {
mytransform = transform;
comp = mytransform.FindChild("BuildingComputer").GetComponent<BuildingComputer> ();
}
// Update is called once per frame
void Update () {
buildtime -= Time.deltaTime;
if (buildtime >= 0) {
built = true;
}
if (built) {
comp.isbuilt = true;
}
}
}[/code2]
if anyone can help me out here
i would really appreciate it
0
Comments
-
Without the code, can you re-explain what you want to happen and what happens?
A PhotonNetwork.Instantiate will create the object for all clients at the position you assign it initially. If you need to do something with the new object on all clients, put a script on the prefab and implement void OnPhotonInstantiate(PhotonMessageInfo info) {}. This is called on the new GameObject when created.0