x


Need help with a build function

I have a build function that is activated from a menu. The user clicks on a button on the menu, which closes the menu and activates a holo effect on the cursor point. When the user clicks on a spot on the terrian, the object will be placed (in the code below it should be a watchtower).

//buildscript
var height : int = 0;
var watchtower : Transform;
var watchtower_holo : Transform;
var buildwatchtower : boolean;

if (buildwatchtower) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
       if(Physics.Raycast(ray,hit)){
        watchtower_holo.position = Vector3(hit.point.x,height,hit.point.z);
       }
}

if (buildwatchtower) {
    if(Input.GetMouseButton(0))
    {
        Buildwatchtower0();
        buildwatchtower = false;
    }
}


function buildmenu0 (WindowID : int) {
//standard buildings    
if (GUI.Button (Rect(140,20,100,20), "Watch Tower")){
claimbuilding = false;
buildwatchtower = true;
buildmenu = false;
Screen.lockCursor = true;
    }

}

function Buildwatchtower0() {
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit, 100)) 
        {
            Debug.DrawLine (ray.origin, hit.point);
            Instantiate(watchtower,hit.point,Quaternion.identity);
        }

}

According to the feedback i'm getting, the menu system is working fine, but the holo effect and subsequently the building placement isn't working. I have probably missed something really obvious, but any help you can give would be great.

more ▼

asked May 14 '12 at 10:53 AM

darkironphoenix gravatar image

darkironphoenix
10 3 5 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
    }
}
more ▼

answered May 14 '12 at 12:44 PM

bompi88 gravatar image

bompi88
741 2 5

Using your script, i get the following error:

Assets/Scripts/Gameplay/Buildscript2.js(73,32): BCE0019: 'point' is not a member of 'Object'.

Which would be this line: Instantiate(watchtower,hit.point + Vector3(0,watchtowerHeight,0),Quaternion.identity);

I will also add, the reason why i am locking the cursor is because i need it to lock in the centre of the screen, like an fps (player is in first person view).

May 14 '12 at 02:49 PM darkironphoenix

Are you sure that you have not edited my script somehow? Works perfectly over here. Maybe you are missing that i'm sending the RaycastHit variable hit to the function Buildwatchtower0(hit)?

May 14 '12 at 03:51 PM bompi88

Aha...its working now.

Having one of those days i think :)

Thanks for the help

May 14 '12 at 04:11 PM darkironphoenix
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1673
x660
x10

asked: May 14 '12 at 10:53 AM

Seen: 313 times

Last Updated: May 14 '12 at 04:11 PM