Grid of furniture in game like The Sims
so this is my first question here :D and I need help with a script that can create an invisible grid that attaches the furniture to its space, just like in The Sims, when you are buying the furniture and it can check if there is already a gameobject there. Please Help
Answer by Robinmtb · Jan 15, 2012 at 12:35 AM
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
Answer by macdude2 · May 20, 2011 at 04:04 AM
what you can do is either put a trigger on the bottom of the furniture and then set the OnTriggerEnter() function to what ever you want to do, say turn the grid red. Or you can do linecasts or raycasts to check to see if there is anything around your furniture.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082656im_/https://answers.unity.com/themes/thub/images/avi.jpg)