Hi I was wondering if anyone could help translate these c# scripts to unity script if possible please.
I’ve done some of the work but most of it is a little confusing to me.
Here’s the C# scripts
Building manager.
using UnityEngine;
using System.Collections;
public class BuildingManager : MonoBehaviour {
public GameObject[] buildings;
private BuildingPlacement buildingPlacement;
// Use this for initialization
void Start () {
buildingPlacement = GetComponent<BuildingPlacement>();
}
// Update is called once per frame
void Update () {
}
void OnGUI() {
for (int i = 0; i <buildings.Length; i ++) {
if (GUI.Button(new Rect(Screen.width/20,Screen.height/15 + Screen.height/12 * i,100,30), buildings[i].name)) {
buildingPlacement.SetItem(buildings[i]);
}
}
}
}
Building placement
using UnityEngine;
using System.Collections;
public class BuildingPlacement : MonoBehaviour {
public float scrollSensitivity;
private PlaceableBuilding placeableBuilding;
private Transform currentBuilding;
private bool hasPlaced;
public LayerMask buildingsMask;
private PlaceableBuilding placeableBuildingOld;
// Update is called once per frame
void Update () {
Vector3 m = Input.mousePosition;
m = new Vector3(m.x,m.y,transform.position.y);
Vector3 p = camera.ScreenToWorldPoint(m);
if (currentBuilding != null && !hasPlaced) {
currentBuilding.position = new Vector3(p.x,0,p.z);
if (Input.GetMouseButtonDown(0)) {
if (IsLegalPosition()) {
hasPlaced = true;
}
}
}
else {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit = new RaycastHit();
Ray ray = new Ray(new Vector3(p.x,8,p.z), Vector3.down);
if (Physics.Raycast(ray, out hit,Mathf.Infinity,buildingsMask)) {
if (placeableBuildingOld != null) {
placeableBuildingOld.SetSelected(false);
}
hit.collider.gameObject.GetComponent<PlaceableBuilding>().SetSelected(true);
placeableBuildingOld = hit.collider.gameObject.GetComponent<PlaceableBuilding>();
}
else {
if (placeableBuildingOld != null) {
placeableBuildingOld.SetSelected(false);
}
}
}
}
}
bool IsLegalPosition() {
if (placeableBuilding.colliders.Count > 0) {
return false;
}
return true;
}
public void SetItem(GameObject b) {
hasPlaced = false;
currentBuilding = ((GameObject)Instantiate(b)).transform;
placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding>();
}
}
PlaceableBuilding
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlaceableBuilding : MonoBehaviour {
[HideInInspector]
public List<Collider> colliders = new List<Collider>();
private bool isSelected;
public string bName;
void OnGUI() {
if (isSelected) {
GUI.Button(new Rect(Screen.width /2, Screen.height / 20, 100, 30), bName);
}
}
void OnTriggerEnter(Collider c) {
if (c.tag == "Building") {
colliders.Add(c);
}
}
void OnTriggerExit(Collider c) {
if (c.tag == "Building") {
colliders.Remove(c);
}
}
public void SetSelected(bool s) {
isSelected = s;
}
}
Here’s the start of the unityScripts
Building Manager
public var buildings: GameObject[];
private var buildingPlacement: BuildingPlacement;
// Use this for initialization
function Start() {
buildingPlacement = GetComponent.<BuildingPlacement>();
}
// Update is called once per frame
function Update() {
}
function OnGUI() {
for (var i: int = 0; i <buildings.Length; i ++) {
if (GUI.Button(new Rect(Screen.width/20,Screen.height/15 + Screen.height/12 * i,100,30), buildings[i].name)) {
buildingPlacement.SetItem(buildings[i]);
}
}
}
Building Placement
public var scrollSensitivity: float;
private var placeableBuilding: PlaceableBuilding;
private var currentBuilding: Transform;
private var hasPlaced: boolean;
public var buildingsMask: LayerMask;
private var placeableBuildingOld: PlaceableBuilding;
// Update is called once per frame
function Update() {
// Not sure on this part
var m: Vector3 = Input.mousePosition;
m = new Vector3(m.x,m.y,transform.position.y);
var p: Vector3 = camera.ScreenToWorldPoint(m);
if (currentBuilding != null && !hasPlaced) {
currentBuilding.position = new Vector3(p.x,0,p.z);
if (Input.GetMouseButtonDown(0)) {
if (IsLegalPosition()) {
hasPlaced = true;
}
}
}
else {
//not sure on this part
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit = new RaycastHit();
var ray: Ray = new Ray(new Vector3(p.x,8,p.z), Vector3.down);
if (Physics.Raycast(ray,hit,Mathf.Infinity,buildingsMask)) {
if (placeableBuildingOld != null) {
placeableBuildingOld.SetSelected(false);
}
//not sure on this part
hit.collider.gameObject.GetComponent.<PlaceableBuilding>().SetSelected(true);
placeableBuildingOld = hit.collider.gameObject.GetComponent.<PlaceableBuilding>();
}
else {
if (placeableBuildingOld != null) {
placeableBuildingOld.SetSelected(false);
}
}
}
}
}
// Not sure on this part
function IsLegalPosition(): boolean {
if (placeableBuilding.colliders.Count > 0) {
return false;
}
return true;
}
// Not sure on this part
function SetItem(b: GameObject) {
hasPlaced = false;
currentBuilding = (Instantiate as GameObject(b)).transform;
placeableBuilding = currentBuilding.GetComponent.<PlaceableBuilding>();
}
Placable buildings
import System.Collections.Generic; // I think this is the only one I need
@HideInInspector // not sure if I need this
public var colliders: List.<Collider> = new List.<Collider>();
private var isSelected: boolean;
public var bName: String;
function OnGUI() {
if (isSelected) {
GUI.Button(new Rect(Screen.width /2, Screen.height / 20, 100, 30), bName);
}
}
function OnTriggerEnter(c: Collider) {
if (c.tag == "Building") {
colliders.Add(c);
}
}
function OnTriggerExit(c: Collider) {
if (c.tag == "Building") {
colliders.Remove(c);
}
}
function SetSelected(s: boolean) {
isSelected = s;
}
if someone could help it would be much appreciated
thanks in advance.