Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

In game object placement

Discussion in 'Scripting' started by Hoarmurath, Jul 26, 2016.

  1. Hoarmurath

    Hoarmurath

    Joined:
    Mar 22, 2016
    Posts:
    27
    So i my script places a cube when i hit the mouse2 button but if i try to place a cube on another cube the second cube isnt placed next to the first cube but inside it.
    Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Place : MonoBehaviour
    5. {
    6.  
    7.     Ray ray;
    8.     RaycastHit hit;
    9.     public GameObject prefab;
    10.     public GameObject prefab2;
    11.     public int count = 1;
    12.  
    13.     private void Update() {
    14.  
    15.  
    16.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    17.  
    18.         if (Input.GetMouseButtonDown(1))
    19.         {
    20.             if (count > 0)
    21.             {
    22.                 if (Physics.Raycast(ray, out hit, 10000))
    23.                 {
    24.                    
    25.                     count = count - 1;
    26.                     GameObject obj = Instantiate(prefab, new Vector3(hit.point.x, hit.point.y, hit.point.z), Quaternion.identity) as GameObject;
    27.                    
    28.  
    29.                 }
    30.             }
    31.      }
    32.         if (Input.GetMouseButtonDown(0))
    33.         {
    34.             count = count + 1;
    35.         }
    36.   }
    37.  
    38. }
    39.  
    40.  
     

    Attached Files:

  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    you need to do an offset...
    Code (csharp):
    1.  
    2. GameObject obj = Instantiate(prefab) as GameObject;
    3. var point =hit.point + hit.normal * 0.5f;
    4. obj.transform.position = point;
     
  3. Hoarmurath

    Hoarmurath

    Joined:
    Mar 22, 2016
    Posts:
    27
    little bit better but still doesnt fix the problem.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if you want the cubes to stack neatly ontop of each other (so the edges are aligned) you aren't going to be placing them based on the hit.point, but on the hit.transform.position
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1. GameObject obj = Instantiate(prefab) as GameObject;
    2. var point =hit.transform.position + hit.normal;
    3. obj.transform.position = point;[code]
     
    LeftyRighty likes this.
  6. Hoarmurath

    Hoarmurath

    Joined:
    Mar 22, 2016
    Posts:
    27
    first cube is always placed on 0,0,0 cords
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    if they are 1x1x1 cubes, then the script i gave will work perfectly, it doesn't matter where the first one is placed.

    basically, you get the item you hit, then get it's position, then add the normal to that position, and it will form fit the block to the next one.

    NOTE: If you are trying to do Minecraft... this is NOT!!! how to do it.
     
  8. Hoarmurath

    Hoarmurath

    Joined:
    Mar 22, 2016
    Posts:
    27
    i think with minecraft i would be easier cuz the everthing is block in my case if i try to place them on the plane they are place on 0,0,0 cords