Dear UnityAnswers community,
I’ve looted a bit of code from someone else to help facilitate a little dungeon creator I’m working on. The code is placed in the main player camera and it spawns a little cube when the player clicks on the mouse.
Reference:
var building : GameObject;
var gridX = 5;
var gridY = 5;
var spacing = 2.0;
function Update ()
{
if(Input.GetMouseButtonDown(0))
{
Build();
}
}
function Build()
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100))
{
Debug.DrawLine (ray.origin, hit.point);
Instantiate(building,hit.point,Quaternion.identity);
}
}
What I need to work out is how best to get the cube to follow some kind of grid and only create a new prefab along certain X-Y-Z axis!
May I point out that this isn’t going to be a MineCraft clone- I’m simply using the cube as a starting point and will actually replace this with in game prefabs such as walls, doors, etc… However, I suppose the principal is the same although the objects I have in mind may be several blocks high/long/wide for certain ‘props’.
Thanks in advance