I did some adjustments to your script. Made the raycast layer dependent, etc. Tried to comment as much as possible in the script. You need to set your plane/terrain to layer Ground, or it would not work.
JavaScript version:
//buildscript.js
// Watchtowers height over ground
var watchtowerHeight : float = 0;
// Holos height over ground
var holoHeight : float = 0;
// Watchtower prefab
var watchtower : GameObject;
// HoloPrefab
var watchtowerHoloPrefab : GameObject;
// The Holo Object
private var watchtowerHolo : GameObject;
// Is it time to build?
private var buildwatchtower : boolean = false;
// Should the build menu appear
private var buildmenu : boolean = true;
// Size and position of the GUI Window
private var windowRect : Rect = Rect (20, 20, 120, 50);
// LayerInfo for Raycasting
private var LayerGround;
function Start () {
// bit shift the index of the layer to get a bit mask
LayerGround = 1 << LayerMask.NameToLayer("Ground");
// Instantiates a Holo at Start
watchtowerHolo = Instantiate(watchtowerHoloPrefab,new Vector3(0,0,0),Quaternion.identity);
// And inactivates it
watchtowerHolo.active = false;
}
function OnGUI () {
// Register the window, if buildmenu is true
if(buildmenu) {
windowRect = GUI.Window (0, windowRect, buildmenu0, "Build");
}
}
function Update() {
// if it's time to build, do raycast and position the holo and check if mouse down to build
if (buildwatchtower) {
// Raycast the layer "Ground"
var ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit,Mathf.Infinity,LayerGround)) {
// if holo is not active, activate it
if(!watchtowerHolo.active)
watchtowerHolo.active = true;
// Positioning the holo
watchtowerHolo.transform.position = new Vector3(hit.point.x,holoHeight + hit.point.y,hit.point.z);
// If mouse button down, build a watchtower
if(Input.GetMouseButton(0))
{
Buildwatchtower0(hit);
}
}
}
}
function buildmenu0 (WindowID : int) {
if (GUI.Button (Rect(10,20,100,20), "Watch Tower")){
// Now you can build
buildwatchtower = true;
// Hides the build menu
buildmenu = false;
// You should hide the cursor, not lock it
Screen.showCursor = false;
}
}
function Buildwatchtower0(hit) {
// instantiate a building
Instantiate(watchtower,hit.point + Vector3(0,watchtowerHeight,0),Quaternion.identity);
// turn holo to inactive
watchtowerHolo.active = false;
// not building any more
buildwatchtower = false;
// Show build menu
buildmenu = true;
// Show cursor
Screen.showCursor = true;
}
C# version:
//buildscript.cs
using UnityEngine;
using System.Collections;
public class buildscript : MonoBehaviour {
// Watchtowers height over ground
public float watchtowerHeight = 0;
// Holos height over ground
public float holoHeight = 0;
// Watchtower prefab
public GameObject watchtower;
// HoloPrefab
public GameObject watchtowerHoloPrefab;
// The Holo Object
private GameObject watchtowerHolo;
// Is it time to build?
private bool buildwatchtower = false;
// Should the build menu appear
private bool buildmenu = true;
// Size and position of the GUI Window
private Rect windowRect = new Rect (20, 20, 120, 50);
// LayerInfo for Raycasting
private LayerMask LayerGround;
void Start () {
// bit shift the index of the layer to get a bit mask
LayerGround = 1 << LayerMask.NameToLayer("Ground");
// Instantiates a Holo at Start
watchtowerHolo = (GameObject) GameObject.Instantiate(watchtowerHoloPrefab,new Vector3(0,0,0),Quaternion.identity);
// And inactivates it
watchtowerHolo.active = false;
}
void OnGUI () {
// Register the window, if buildmenu is true
if(buildmenu) {
windowRect = GUI.Window (0, windowRect, buildmenu0, "Build");
}
}
void Update() {
// if it's time to build, do raycast and position the holo and check if mouse down to build
if (buildwatchtower) {
// Raycast the layer "Ground"
Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit,Mathf.Infinity,LayerGround)) {
// if holo is not active, activate it
if(!watchtowerHolo.active)
watchtowerHolo.active = true;
// Positioning the holo
watchtowerHolo.transform.position = new Vector3(hit.point.x,holoHeight + hit.point.y,hit.point.z);
// If mouse button down, build a watchtower
if(Input.GetMouseButton(0))
{
Buildwatchtower0(hit);
}
}
}
}
void buildmenu0 (int WindowID) {
if (GUI.Button (new Rect(10,20,100,20), "Watch Tower")){
// Now you can build
buildwatchtower = true;
// Hides the build menu
buildmenu = false;
// You should hide the cursor, not lock it
Screen.showCursor = false;
}
}
void Buildwatchtower0(RaycastHit hit) {
// instantiate a building
GameObject.Instantiate(watchtower,hit.point + new Vector3(0,watchtowerHeight,0),Quaternion.identity);
// turn holo to inactive
watchtowerHolo.active = false;
// not building any more
buildwatchtower = false;
// Show build menu
buildmenu = true;
// Show cursor
Screen.showCursor = true;
}
}