- Home /
Minecraft like building?
I have a building mechanism that allows you to place blocks, it's all free and custom, though i want it to be in a grid, so when you place it, it'll be exactly the same to the grid.
i didn't try anything yet, but this is the script i've been using to place blocks:
using UnityEngine;
using System.Collections;
public class PlaceBlock : MonoBehaviour
{
Ray ray;
RaycastHit hit;
public GameObject prefab;
public GameObject prefab2;
// 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.GetKeyDown(KeyCode.X))
{
GameObject obj = Instantiate(prefab, new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z), Quaternion.identity) as GameObject;
}
if (Input.GetKeyDown(KeyCode.Z))
{
GameObject obj = Instantiate(prefab2, new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z), Quaternion.identity) as GameObject;
}
}
}
}
i have no idea how to acomplish it, i need help
this is how it looks so far
Answer by MT369MT · May 22, 2018 at 06:01 PM
Hi, You must round your x, y and z values for example: GameObject obj = Instantiate(prefab2, new Vector3(Mathf.Round(hit.point.x - 0.5f) + 0.5f, Mathf.Round(hit.point.y - 0.5f) + 0.5f, Mathf.Round(hit.point.z - 0.5f) + 0.5f);
It works for the most part. though, with the "z" input, the blocks don't stack, while the "x" input does.. and oddly the blocks cannot be put next to eachother using eachother.. i'll put in a video for better details link text
Thanks for helping -Jessa
This is the current script, btw
using UnityEngine;
using System.Collections;
public class PlaceBlock : $$anonymous$$onoBehaviour
{
Ray ray;
RaycastHit hit;
public GameObject prefab;
public GameObject prefab2;
// 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.Get$$anonymous$$eyDown($$anonymous$$eyCode.X))
{
GameObject obj = Instantiate(prefab, new Vector3($$anonymous$$athf.Round(hit.point.x - 0.5f) + 0.5f, $$anonymous$$athf.Round(hit.point.y - 0.5f) + 0.5f, $$anonymous$$athf.Round(hit.point.z - 0.5f) + 0.5f), Quaternion.identity) as GameObject;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z))
{
GameObject obj = Instantiate(prefab2, new Vector3($$anonymous$$athf.Round(hit.point.x - 0.5f) + 0.5f, $$anonymous$$athf.Round(hit.point.y - 0.5f) + 0.5f, $$anonymous$$athf.Round(hit.point.z - 0.5f) + 0.5f), Quaternion.identity) as GameObject;
}
}
}
}
Have the cubes some scripts? And have they also a rigidbody or other things?
yess, they do. but the one with ridgid body works way better then the one without
But the scripts and the ridgit body aren't the problem, as i also tried with normal cubes, they didn't work propperly either.
$$anonymous$$athf.Round round the .5 values to the nearest EVEN integer so 1.5 will be rounded to 2 and 2.5 also to 2. This is why sometimes the cubes go inside the other cubes. And obviously the raycast doesn’t know if he is touching on the left side or right side of the cube. You can try to do it with the normals of the colliders for example:
GameObject obj = Instantiate (prefab, hit.collider.transform.position + hit.normal, Quaternion.identity);
I've got it working now, i just enlarged the cubes with 0.01 and now it works all nice and good. Thanks for your help!
Answer by anthot4 · May 23, 2018 at 11:14 AM
@MT369MT is right, you need to mathf.clamp where the raycasting is hitting the ground. The reason the blocks don't stack is you need a "top collider" trigger on the top of the bricks to identify for the raycast that it is hitting the top of the brick. The game I am currently working on is similar to yours, I have got it working so that the bricks are stacked on top of each other and got the player able to rotate the bricks too.
Your answer
Follow this Question
Related Questions
Lego brick placement 0 Answers
Up to date Unity building placement code/tutorials? 1 Answer
How to make a grid and able to place buildings on? 0 Answers