- Home /
Minimum distance between cubes
I use this script:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
Ray ray;
RaycastHit hit;
public GameObject prefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
ray=Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit))
{
if(Input.GetKey(KeyCode.Mouse0))
{
GameObject obj=Instantiate(prefab,new Vector3(hit.point.x,hit.point.y,hit.point.z), Quaternion.identity) as GameObject;
}
}
}
}
But, when I click, as well as create me 3 cubes at a time, it makes them all attached >.<
How can I make sure that between a block and the other there is sufficient space to display Compleat well and cubes? Like minecraft, for inted D:
PS: Sorry for bad english.. but, all know google translate ^^"
Answer by MakeCodeNow · Mar 09, 2014 at 04:20 AM
In a minecraft type game, there is a global, 3D grid of voxels, each the size of one cube. In that kind of world, you just find the center of the voxel nearest the ray intersection (that is not the cube you clicked on).
Hello, the script used is old :C Now use I use this : #pragma strict
var spawnAreas : Layer$$anonymous$$ask; //Layers where you can spawn objects, make sure to fill this out
var destroyableObjects : Layer$$anonymous$$ask; //Objects that is ok to destroy, make sure to fill this out
var spawnPrefab : GameObject; //The object you want to spawn, in case you didn't assign the script creates a primitive
var grid : Vector3 = Vector3(1,1,1); //The grid you want to instantiate objects by
var colliderSize : Vector3 = Vector3(1,1,1); //The collider size of the object
var cam : Camera; //The camera you're using, in case you didn't assign one the script assigns the $$anonymous$$ain Camera
private var range : float = 100.0; //Don't use infinity, use the camera's far clipping plane if you need to
private var hit : RaycastHit;
function Start () {
cam = Camera.main;
}
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0))
Build();
if (Input.Get$$anonymous$$ouseButtonDown(1))
Erase();
}
function Build () {
if (HitBlock(spawnAreas)) {
if (spawnPrefab==null) {
//In case you didn't have a prefab we spawn a primitive
spawnPrefab = GameObject.CreatePrimitive(PrimitiveType.Cube);
spawnPrefab.transform.position = Grid(hit.point);
} else {
//You have a prefab, let's spawn it
var spawnObject = Instantiate(spawnPrefab, Grid(hit.point), Quaternion.identity);
}
}
}
function Erase () {
if (HitBlock(destroyableObjects))
Destroy(hit.transform.gameObject);
}
function HitBlock (mask : Layer$$anonymous$$ask) : boolean {
var ray = cam.ScreenPointToRay (Input.mousePosition);
return Physics.Raycast(ray, hit, range, mask);
}
function Grid (pos : Vector3) : Vector3 {
return Vector3($$anonymous$$athf.Round(pos.x/grid.x)*grid.x-(colliderSize.x/2), $$anonymous$$athf.Round(pos.y/grid.y)*grid.y+(colliderSize.y/2), $$anonymous$$athf.Round(pos.z/grid.z)*grid.z-(colliderSize.z/2));
}
But, thanks for the reply ^^
Your answer
Follow this Question
Related Questions
Problem with NGUI Removing and destroying Prefabs UIobjects in runTime 0 Answers
prefab Changes are not always applied in all scenes 0 Answers
Accesing variable from another class for instantiated gameobjects 0 Answers
instantiated prefab is invisible in game view but exists in the scene 0 Answers
GameObject variable points to instance instead of prefab when prefab gets instantiated 2 Answers