- Home /
Instantiate at hit.point
Hello all!
I've been trying to instantiate a gameobject at the mouse position. I know to do this I need to use RaycastHit, but when I try to instantiate with Instantiate(waypoint, hit.point, Quaternion.identity);
, it doesn't work. The console doen't say that there are any errors, but when I try to use the script, nothing happens. Any ideas?
Full script:
#pragma strict
var waypoint: GameObject;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
if (hit.rigidbody != null)
Instantiate(waypoint, hit.point, Quaternion.identity);
}
}
}
your code only works if you point at a object with rigidbody
Answer by MakeCodeNow · Feb 15, 2014 at 04:27 PM
Your code requires that the hit object have a rigid body but many objects only have colliders. Are you sure that you want to limit this hit to rigid bodies only?
Answer by game91 · Feb 16, 2014 at 01:44 AM
Thanks all! I changed it to if (hit.collider != null)
and that solved the problem.