Best Way To Do This?

I am working on RTS, and part of it is placing buildings. To do so, you need to move your mouse over the part of the terrain on which you want to place the building. I am using a blob projector to display the building footprint on the terrain. However, I cannot figure out how to make the footprint always appear under the mouse. I’m trying to use ScreenToWorldPoint to do it, and that does result in the projector moving around, but I can’t figure how to get the footprint to stay with the mouse cursor. Does anyone know how to do this?

Dunno if it’s the proper way but seems to work for me:

        Ray r = camera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hinfo;
        Physics.Raycast(r, out hinfo, 1000); // whatever distance is enough to cover your terrain height.


        whatever.position = hinfo.point;

Incidently, setting the terrain on a layer mask seems to return only false when I do a raycast on it. Anyone know why that is? 'cause the solution has a problem that it also returns the points above any rigidbody standing ontop of the terrain!

with regardins to the mask:

make a layer with your terrain that you wish to raycast on e.g. layer number 8.
Then in this case do like this.
C# code.

//layer 8 your terrain layer.
LayerMask mask = (1<<8);
RaycastHit hit; 
Ray ray = camera.ScreenPointToRay(Input.mousePosition); 
Physics.Raycast(ray, out hit, Mathf.Infinity, mask);

Oh I see, it’s an actual bitmask! Too used to C# over C++ :wink:

Ok. Well, this gets me close, but we’re not there yet. I can get close to the mouse position, but it is still offset. The ray is hitting the terrain at the right point, but the positioning of the object seems to be offset, and I’m not sure why. At any rate, it isn’t lining up under the mouse. Any thoughts?

I have included two screenies, and the script I am using. The first picture is a debug line drawn to show where the ray is hitting relative to the object’s position (I am no longer using a projector. I’m just using the object that will be placed). However, in the second screenie, you can see that the hitPoint’s position and the object’s position are the same in the inspector.

Anyone have any ideas as to why there is a discrepancy?

var village : GameObject;
private var footPrint : GameObject;
var hitPoint : Vector3;
var villagePosition : Vector3;

function Footprint()
{
	var mask : LayerMask = (1<<8);
	var hit : RaycastHit;
	var ray = camera.ScreenPointToRay(Input.mousePosition);
	Physics.Raycast(ray,hit,Mathf.Infinity, mask);
	Debug.DrawLine(transform.position,hit.point,Color.red);
	if(footPrint == null)
		footPrint= Instantiate(village);
	footPrint.transform.position = hit.point;
	hitPoint = hit.point;
	villagePosition = footPrint.transform.position;
}

function Update()
{
	Footprint();
}

177396--6342--$sceneview_149.jpg
177396--6343--$inspectorview_154.jpg

Ok. I figured out the problem, and while I know what I think is a hack workaround, I was wondering if there is another way to do it. The pivot point of the object is positioned at the correct place. The center of the object is not. The work around it to evenly distribute the buildings around the center of the village object, but really?

Why does it want to use the pivot point for positioning and not the object’s center? I tried toggling the pivot/center button, but it doesn’t do anything but change which I see. It is still using the vilage object’s pivot point. How can I either keep the pivot point at the center of the object, or force it to position to object according to its center, and not its pivot?