using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuildingSystem : MonoBehaviour
{
public LayerMask detailLayer;
public bool IsBuilding;
void Update()
{
if (IsBuilding && Input.GetMouseButtonDown(1))
{
Destroy();
}
}
public void Destroy()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 20, detailLayer))
{
Destroy(hit.transform.gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestructibleObject : MonoBehaviour
{
void OnMouseDown()
{
Destroy(gameObject);
}
}
public bool IsRemove;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddComponent : MonoBehaviour {
public GameObject buildingCamera;
Rigidbody rig;
void Start()
{
buildingCamera = GameObject.FindGameObjectWithTag("MainCamera");
}
void Update ()
{
BuildingSystem BS = buildingCamera.GetComponent<BuildingSystem>();
if (BS.IsRemove)
{
if (rig == null)
{
rig = gameObject.AddComponent<Rigidbody>();
rig.constraints = RigidbodyConstraints.FreezeAll;
}
}
if (!BS.IsRemove)
{
if (rig != null)
Destroy(rig);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuildingHUB : MonoBehaviour {
public GameObject buildingCamera;
public GameObject currentPreview;
BuildingSystem buildingSystem;
void Start()
{
buildingSystem = buildingCamera.GetComponent<BuildingSystem>();
}
void Update ()
{
currentPreview = buildingSystem.currentpreview.gameObject;
if (buildingSystem.IsBuilding && Input.GetKeyDown(KeyCode.Delete))
{
ToRemove();
}
if (buildingSystem.IsRemove && Input.GetKeyDown(KeyCode.Escape))
{
RemoveToEscape();
}
}
public void ToRemove()
{
buildingSystem.IsRemove = true;
buildingSystem.IsBuilding = false;
currentPreview.SetActive(false);
}
public void RemoveToEscape()
{
buildingSystem.IsRemove = false;
buildingSystem.IsBuilding = true;
currentPreview.SetActive(true);
}
}
public void Destroy() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 20, detailLayer)) {
// теперь в hit что-то будет
}
}