How do you prevent instantiation of objects inside each other with proper orientation?
Hello everyone, I am making an application that is supposed to spawn blocks on click and place them next to or above each other in a grid-like fashion - Like minecraft - on a floor made of a single cube that is instantiated multiple times depending on the wanted dimension, but i am facing multiple problems and I'm going to mention 3 of them.
First, when i spawn an object which is a cube prefab but the cubes that are being spawned can overlap or spawn inside each other or the floor.
Second, when i spawn the same prefab it is spawned in a different orientation than the floor.
Third, the spawning always happens at a certain distance from the camera.
For the 1st and 2nd problems i know that it is solved by using Ray Cast but I can't seem to figure out how to use it since i just started with unity.
I would appreciate a quick response! Thanks in advance.
Here is the code for instantiation:
if (Input.GetMouseButtonUp(0))
{
Vector3 mousepos = Input.mousePosition;
mousepos.z = 10;
mousepos = Camera.main.ScreenToWorldPoint(mousepos);
transform.position = new Vector3(Mathf.Round(mousepos.x), Mathf.Round(mousepos.y), Mathf.Round(mousepos.z));
GameObject go = Instantiate(selectedBlocks, transform.position, Quaternion.identity) as GameObject;
go.transform.SetParent(parent.transform);
}
Your answer
Follow this Question
Related Questions
Raycast never gets called 1 Answer
Unity Raycasting Issue 1 Answer
Instantiate a Prefab along the surface of a sphere. C# 0 Answers
Destroy attached shield to player (C#) 2 Answers