Preview selected gameobject at hit.point

Hello everyone!!

I am not going to lie, but I do not have the code for it what I need as of this moment, but I feel it is easier than I am making it.

What I would like help on:

A script which will place a transparent version of my selected block at the hit.normal of where my block is going to be placed.

So I can see where my block will be placed before I even place it.

If anyone out there can help me with this, PLEASE feel free to do so :stuck_out_tongue:
I have looked EVERYWHERE and I have no luck finding anything to fix my issue.

Thank you for reading,
Sincerely,

Bakos133

There is a lot of detail missing from your question. The sample code I’m providing assumes blocks of 1x1x1.

  • Create a cube in the scene visible from the camera (GameObject > Create Other > Cube).
  • Add the tag “Cube” to the tag manager and Tag this cube with the new tag
  • Create a second cube, and name it “Preview”
  • Create a new material. Select the Transparent/Diffuse shader for this material. Select the color and change the alpha to 75 (out of 255).
  • Attach the new material to the ‘Preview’ cube.
  • Turn off or delete the collider on the ‘Preview’ cube.
  • Attach the following script to the ‘Preview’ cube.
  • Select the ‘Preview’ cube in the hierarchy. Drag and drop the original cube (the one tagged ‘Cube’) onto the blockPrefab variable of this script in the Inspector.
  • Add a directional light
  • Hit run.

#pragma strict

var blockPrefab : GameObject;

function Update() {
    var hit : RaycastHit;
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, hit) && hit.collider.tag == "Cube") {
    	var pos = hit.transform.position;
    	pos += hit.normal;
    	renderer.enabled = true;
    	transform.position = pos;
    	if (Input.GetMouseButtonDown(0)) {
    		Instantiate(blockPrefab, pos, Quaternion.identity);
    	}
    }
    else {
    	renderer.enabled = false;
    }
}

Note I added a modified version of MouseOrbit code to he camera so I could test this scrip by adding cubes from all sides. I added this line just inside Update() on the MouseOrbit script:

if (!Input.GetKey(KeyCode.LeftShift)) return;

That way I could hold the shift key to rotate but but easily click on the block when the left shift key was not held down.

Note if you are going to be building larger structures, you don’t want to build them out of individual game objects. Look into voxels:

http://unitycoder.com/blog/2012/10/18/voxel-resources-engines-for-unity/