- Home /
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
Answer by Borgo 1 · Apr 20, 2011 at 02:14 PM
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.
Your answer
Follow this Question
Related Questions
OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer
Making the collider change after being instantiate 2 Answers
Destroy() not working on collision 2 Answers
PhysX and multiple instances at same location 1 Answer
Hi! How can I use the OnTriggerEnter2D function for the game objects that I instantiate? 3 Answers