Hi Tonts!
You can make a grid like that like this:
public var gridObject : Transform; //The floor-object in your project
public var gridElementsX = 6; //Number of grid elements in X-axis
public var gridElementsZ = 4; //Number of grid elements in Z-axis
public var gridElementSizeX = 4; //How long a gridelement's side is in X-axis.
public var gridElementSizeZ = 2; //How long a gridelement's side is in Z-axis.
private var totalGridSizeX = numberOfGridElementsX * gridElementSizeX;
private var totalGridSizeZ = numberOfGridElementsZ * gridElementSizeZ;
private var furniture: Transform; //The furniture you wish to position
function Update (){
//Use RayCast wich will store the position the cursor is pointing at in a RaycastHit, providing your floor has a collider!
var hit : RaycastHit;
var ray = camera.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray,hit,Mathf.Infinity);
furniture.position.x = gridObject.position.x + Mathf.Floor(hit.point.x * (gridElementsX / totalGridSizeX)) * gridElementSizeX;
furniture.position.z = gridObject.position.z + Mathf.Floor(hit.point.z * (gridElementsZ / totalGridSizeZ)) * gridElementSizeZ;
}
That should do the trick to get it to position properly :) Just change the values so they work for you and don't forget that the RayCasting depends on having a collider that "isTrigger" on the floor-object.
I'm sorry if there are any errors in the code. I didn't have time to test it.
Good luck!
/Robin
↧