Cube instantiate problem.

Hey guys, I am trying to make a box placement kind of like minecraft to build objects.
I have my cubes made up of 6 planes because I will need to check diffrent sides for being hit and exploding.

using UnityEngine;
using System.Collections;

public class ConstructionSpacestation : MonoBehaviour
{

    public GameObject SetObject;

    void Start()
    {

    }

    void Update()
    {

        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 2000))
            {
               

                Transform clickedObj = hit.transform;
                Debug.Log(clickedObj);

                GameObject close = Instantiate(SetObject, Vector3.zero, Quaternion.identity) as GameObject;
                close.transform.position = hit.transform.position + hit.normal; 

           
            }
            else
            {
                Debug.Log("nothing");
            }
        }

    }
}

It places the objects but I have no idea why its doing this.

No matter what I do I cant get the boxes to instancing uniformly beside each other either from the top or the side or any other place I raycast hit they move over what ever way.

I tried this with making a instance with a 1,1,1 cube and it works!

I just cant get it to work with the object prefabs I make in max…

Yeah I know its because of that.. I made the cube in planes so I have to figure out how to put on plane on the other plane.

I assume that you have all six planes as children of an empty game object. If so add a new empgy game object as the parent of the current plane paren. Then adjust the position of the child. If all six planes were created in max (rather than the cube being build in Unity out of planes authored in max), then you can go back to max and reset the origin as well.

1 Answer

1

Seems to me that you place the cubes at the exact location of the raycast hits. What you want to do is to place them on some kind of uniform grid. For example, if your cubes were of side length 1, you could just round the hit coords to full integer values on a given plane. :slight_smile:

Is there anyway I can instanciate the new prefab from one of the 6 sides of the plane that is made up of the cube? As for rounding the hit coords I just googled it and one suggestion came up, but it was not what I needed. Not to famailiar with that.

Actually, you want to find the right position to place the cubes at. If your cube sides are of length 1, then all the cube positions must be at distance 1 from each other, for example. So if you have a raycast hitpoint, what I would do, is to find the nearest valid position for that cube. Doing this will let them all be on a uniform grid like they are in Minecraft. :)