Skip to main content
Notice removed Authoritative reference needed by CommunityBot
Bounty Ended with no winning answer by CommunityBot
added 33 characters in body
Source Link
stighy
  • 477
  • 6
  • 32
  • 62

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 ... HowAll my cube prefab are 1x1x1.

How to solvefix 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);
         }
     }
 }

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 ... How to solve 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);
         }
     }
 }

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);
         }
     }
 }
Notice added Authoritative reference needed by stighy
Bounty Started worth 250 reputation by stighy
Source Link
stighy
  • 477
  • 6
  • 32
  • 62

How to instantiate correctly a cube prefab?

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 ... How to solve 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);
         }
     }
 }