0
\$\begingroup\$

In a FPS i'm trying to instantiate cube like the old good minecraft. The problem is that the first cube i instantiate on terrain got y=0 but half of that cube is underground ... All my cube prefab are 1x1x1.

How to fix this problem ?

 void BuildBlock(GameObject block) { if (Physics.Raycast(shootingPoint.position, shootingPoint.forward, out RaycastHit hitInfo)) { if (hitInfo.transform.tag == "Block") { // Instance over other cube Vector3 spawnPosition = new Vector3(Mathf.RoundToInt(hitInfo.point.x + hitInfo.normal.x / 2), Mathf.RoundToInt(hitInfo.point.y + hitInfo.normal.y / 2), Mathf.RoundToInt(hitInfo.point.z + hitInfo.normal.z / 2)); Instantiate(block, spawnPosition, Quaternion.identity, parent); } else { // Instance on terrain Vector3 spawnPosition = new Vector3(Mathf.RoundToInt(hitInfo.point.x), Mathf.RoundToInt(hitInfo.point.y), Mathf.RoundToInt(hitInfo.point.z)); Instantiate(block, spawnPosition, Quaternion.identity, parent); } } } 
\$\endgroup\$
5
  • \$\begingroup\$ Is your game in a grid layout? And if it is half in the ground, it takes the middle of the cube for the location. \$\endgroup\$ Commented Dec 9, 2023 at 21:26
  • \$\begingroup\$ No, it's not in a grid layout.. \$\endgroup\$ Commented Dec 10, 2023 at 0:07
  • \$\begingroup\$ This is missing a few informations. What is not working with the existing answer? How should a cube spawn on a slope? If the cube has to rotate, that's way more complicated compared to part of it can be inside the slope. If everything else in your code is as you want it, all you need to do is 0.5 upwards offset. Since the cubes have a fixed dimension, this can be a fixed value. Or if you have them as a prefab, you can adjust the pivot. \$\endgroup\$ Commented Dec 11, 2023 at 19:49
  • \$\begingroup\$ Your goal is to achieve this without using a grid layout? If so, I have made a similar system in a Tower Defence game before and I wouldn't mind providing an answer. If not and you'd like to create a cube placement system like the one in Minecraft, I recommend you look at the answer provided by agone. \$\endgroup\$ Commented Dec 12, 2023 at 9:46
  • \$\begingroup\$ "No, it's not in a grid layout" then why are you using Mathf.RoundToInt which snaps the position to a regular grid? Try to explain or illustrate what behaviour you want and we can match that. Presumably you already considered adding 0.5 to the y to lift the bottom of the cube up to the hit point. \$\endgroup\$ Commented Dec 12, 2023 at 11:46

2 Answers 2

0
\$\begingroup\$

No, it's not in a grid layout.

All objects upon creation need to align to a fixed sized grid.

The code for alignment (assuming a 40x40 grid):

 spawnPosition = new Vector3(Mathf.RoundToInt(hitInfo.point.x * 40+19)/40, Mathf.RoundToInt(hitInfo.point.y * 40 + 39.9f)/40, Mathf.RoundToInt(hitInfo.point.z * 40 + 19)/40); 

The 'y + 39.9f` gives the vertical alignment on the square above the trace, where the other two axis are centered to the grid.

\$\endgroup\$
0
\$\begingroup\$

Ok, the problem is that Pivot point is in center of every unity primitive, like a cube. To solve, I can create an empty object with pivot point in the bottom corner, than a child cube. Now it works

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.