- Home /
Unet Instantiate
I have this simple building script, where if i press q i can build objects, like a foundation. But this doesnt sync over UNET? How would i make this script sync over unet?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
public class BuildingSystem : MonoBehaviour
{
public List<buildObjects> objects = new List<buildObjects>();
public buildObjects currentobject;
private Vector3 currentpos;
private Vector3 currentrot;
public Transform currentpreview;
public Transform cam;
public RaycastHit hit;
public LayerMask layer;
public float offset = 1.0f;
public float gridSize = 1.0f;
public bool IsBuilding;
void Start()
{
ChangeCurrentBuilding(0);
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
if(Input.GetKeyDown("q"))
{
IsBuilding = true;
}
else if(Input.GetKeyDown("e"))
{
IsBuilding = false;
}
if (IsBuilding == false)
{
currentpreview.gameObject.SetActive(false);
}else
{
currentpreview.gameObject.SetActive(true);
}
if (IsBuilding == true)
{
startPreview();
}
if (Input.GetButtonDown("Fire1"))
{
Build();
}
if (Input.GetKeyDown("0") || Input.GetKeyDown("1") || Input.GetKeyDown("2"))
{
switchCurrentBuilding();
}
}
public void switchCurrentBuilding()
{
for (int i = 0; i < 3; i++)
{
if(Input.GetKeyDown("" + i))
{
ChangeCurrentBuilding(i);
}
}
}
public void ChangeCurrentBuilding(int cur)
{
currentobject = objects[cur];
if(currentpreview != null)
{
Destroy(currentpreview.gameObject);
}
GameObject curprev = Instantiate(currentobject.preview, currentpos, Quaternion.Euler(currentrot)) as GameObject;
currentpreview = curprev.transform;
}
public void startPreview()
{
if (Physics.Raycast(cam.position, cam.forward, out hit, 10, layer))
{
if(hit.transform != this.transform)
{
showPreview(hit);
}
}
}
public void showPreview(RaycastHit hit2)
{
currentpos = hit2.point;
currentpos -= Vector3.one * offset;
currentpos /= gridSize;
currentpos = new Vector3(Mathf.Round(currentpos.x), Mathf.Round(currentpos.y), Mathf.Round(currentpos.z));
currentpos *= gridSize;
currentpos += Vector3.one * offset;
currentpreview.position = currentpos;
if (Input.GetButtonDown("Fire2"))
{
currentrot += new Vector3(0,90,0);
}
currentpreview.localEulerAngles = currentrot;
}
public void Build()
{
PreviewObject PO = currentpreview.GetComponent<PreviewObject>();
if (PO.IsBuildable)
{
Instantiate(currentobject.prefab, currentpos, Quaternion.Euler(currentrot));
}
}
}
[System.Serializable]
public class buildObjects
{
public string name;
public GameObject prefab;
public GameObject preview;
public int Gold;
}
Comment
Your answer
Follow this Question
Related Questions
Syncing Random vars for NPCs 0 Answers
Can i Call Additive Scene On Network 1 Answer
UNet NetworkServer max connections 4 Answers
Multiplayer Object spawned by client does not show up on host 1 Answer
Multiplayer objects isn't equals. 1 Answer