- Home /
Raycast instantiating object to grid
As you can see in the image attached below, I can Instantiate my object (Cube1) to my Raycast position. The problem is when I create the object it is placed from the center of the obj. Even when clicking on another block the same will happen. My desired effect would be to place them in a 3D grid position and not have one placed through or in another block area even when clicking on a side of a placed block.
I would like to keep the un-named plane at the bottom just for the initial hit area.
Outcome: Sort of a block building effect
This script is place on camera and I do not have a player in scene.
var Cube1 : Transform;
static var InsObj : int = 0;
function Update () {
if (InsObj==1){
var mousex = Input.mousePosition.x;
var mousey = Input.mousePosition.y;
var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 50)) {
}
if ( Input.GetMouseButtonDown(0) ){
var create = Instantiate(Cube1, hit.point, Quaternion.identity);
}
}
}
The variable "InsObj" is run by another script saying the create object on mouse click is active once the button is selected. So is of no importance to the placement.
Thank you for reading through all of this and hopefully someone can help :)