- Home /
Placing Buildings
has anyone here ever played Empire Earth? If so How would i go about making a script where i could place buildings on the ground with the mouse (Like Empire Earth)? If you haven't ever seen/Played it, How would i be able to from a birds eye view be able to places buildings on the terrain? (The Buildings would be just clones of 3D models with box collides) Oh and a grid would be great! but if that's to hard, it doesn't matter.
Answer by Piflik · Jan 01, 2013 at 08:22 PM
You would have to do a raycast to the mouse position and you would get the collision with the ground plane (you can put the ground plane on a new layer and tell the ray to only collide with that layer, using a layer mask). You can then instantiate a new object at that position, or move an existing object there.
If you want to have a grid, this would also be possible. Just compare the position with the position of the tiles and chose the lowest distance...the actual implementation of this test might be a bit complicated, just iterating through an array would be suboptimal...I'd suggest a Quad-Tree, but I guess that's too much for a beginner...
For the raycasting you can have a look at this...it's UnityScript...
private var mousPos : RaycastHit;
private var positionOnGroundplane : Vector3;
private var layerMask = 1 << 8;
function Update() {
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), mousPos, Mathf.Infinity, layerMask)){
positionOnGroundplane = mousPos.point;
}
}
I cut this from an old script of mine...not sure if it is bugfree, since I trimmed a whole lot of it away.
Oh, sorry...I added a new Vector3 variable and accidentally used c# syntax...fixed it now.
So how do i work it? what do i attach the script to? How do i set the thing it places?
Oh And would i be able to place more then one thing? like press "1" to place somthing and "2" to place another thing?
The script is not complete. All it does is a raycast and get the point on the ground plane. What you do with that point is up to you. You can place whatever you want there.
Where you place this script doesn't matter, either, it just needs to be on an object that is present in the scene. The camera would make sense, or an empty that is used as a 'Build-Objects-$$anonymous$$anager'. Also you would probably not do the raycast on Update, but in a separate function that is called on $$anonymous$$ouse Input. The script was mainly meant to give you an idea how raycasts work.
Answer by TargonStudios · Jan 03, 2013 at 06:28 PM
Ok Well Ive found a script And did a little to it, it works!... but not how i want it to. When i try to place a building, it places it next to some other object (Another building for example) and not where i clicked. Ive tried layers, everything! (I found this script on a Minecraft clone thing if that helps) heres the script:
var blockLayer : LayerMask = 1;
var range : float = Mathf.infinity;
var hit : RaycastHit;
var Building : GameObject;
function Update() {
if (Input.GetMouseButtonDown(0)) Build();
}
function Build() {
if (HitBlock()) {
var cube = Instantiate(Building);
cube.transform.position = hit.transform.position + hit.normal;
}
}
function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}