using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class buildingsystem : MonoBehaviour
{
public List<buildObjects> objects = new List<buildObjects>();
public buildObjects currentobjects;
private Vector3 currentpos;
public Transform currentpreview;
public RaycastHit hit;
public LayerMask layer;
public Transform cam;
public float offset = 1.0f;
public float gridSize = 1.0f;
public bool isBuilding;
private Vector3 currentrot;
void Start()
{
// currentobjects = objects[0];
ChangeCurrentBuilding(0);
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
if(isBuilding)
{
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)
{
currentobjects = objects[cur];
if (currentpreview != null)
Destroy(currentpreview.gameObject);
GameObject curprev = Instantiate(currentobjects.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(currentobjects.prefab, currentpos, Quaternion.Euler(currentrot));
}
}
[System.Serializable]
public class buildObjects
{
public string name;
public GameObject prefab;
public GameObject preview;
public int gold;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PreviewObject : MonoBehaviour {
public objectsorts sorts;
public List<Collider> col = new List<Collider>();
public Material green;
public Material red;
public bool isBuildable;
public bool second;
public PreviewObject childcol;
public Transform graphics;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == 9)
col.Add(other);
}
void OnTriggerExit(Collider other)
{
if(other.gameObject.layer == 9)
col.Remove(other);
}
void Update()
{
if(!second)
changecolor();
}
public void changecolor()
{
if (sorts == objectsorts.foundation)
{
if (col.Count == 0)
isBuildable = true;
else
isBuildable = false;
}
else
{
if(col.Count == 0 && childcol.col.Count > 0)
isBuildable = true;
else
isBuildable = false;
}
if (isBuildable)
{
foreach(Transform child in graphics)
{
child.GetComponent<Renderer>().material = green;
}
}
else
{
foreach (Transform child in graphics)
{
child.GetComponent<Renderer>().material = red;
}
}
}
}
public enum objectsorts
{
normal,
foundation,
floor
}
Its a building system that im not happy with but its literally the only tutorial i could find on youtube.
what the system is missing is kind of flexibility, what i mean by flexibility is that this is a very rough system i would like it to snap to blocks not have a empty space between blocks when im placing them together. and also this system doesnt allow to put the “blocks” into terrain to some degree and it doesnt let me build ontop of each “block” i would like it to be similiar to space engineers.