hi,
I’m trying to create a small rts where you can place buildings.
I made this script but the buildings are not attached to the terrain.
how can I do to ensure that buildings can be built only on the terrain?
I would also like that the buildings are not overlapped…
var Icon1 : Texture2D;
var Icon2 : Texture2D;
var Icon3 : Texture2D;
var Edificio1:GameObject ;
var Edificio2:GameObject ;
var Edificio3:GameObject ;
var building;
var isHoverGUI : boolean = false;
var rect : Rect = Rect (50,200, 10, 400);
function OnGUI ()
{
if (GUI.Button (Rect (50,200, 100,100), GUIContent( Icon1, "") ))
building = Edificio1;
if(GUI.Button (Rect (50,320, 100,100), GUIContent( Icon2, "") ))
building = Edificio2;
if(GUI.Button(Rect ( 50,440, 100,100), GUIContent( Icon3, "") ))
building = Edificio3;
isHoverGUI = rect.Contains( Event.current.mousePosition );
}
function Update ()
{
if(Input.GetMouseButtonDown(0) && !isHoverGUI )
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var mousePos = Input.mousePosition;
var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
Instantiate(building,objectPos,Quaternion.identity);
}
}