Snap to Surface

Hey all,

I am currently working on a game that will be used to train home installers about a new product. I would like them to be able to place devices around the home. I currently have a buggy set up and would like to change it. I need an instanced object to snap to a surface of a colliding object. Any idea how to approach this issue? Is there a way to snap to the surface normal?

Thanks in advance Jim S

var hit : RaycastHit;
if (Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition),  hit, 100)) {
    target.rotation =  Quaternion.FromToRotation (Vector3.up, hit.normal);
    target.position = hit.point;
}

target is the object to place.

If your object have a collider, mark the layer as "ignore raycast". The raycast is like a laser that you point to a place, if it hits a collider, some informations will be returned.

If your object have a collider, the raycast can found this object, instead the place.