- Home /
mouse Input Instantiate
i start looking for an example from the Script Reference for Instantiate gameObject with a mouse clicking but is there own example isn't working.
after i find this in here http://answers.unity3d.com/questions/29214/explosion-at-mouse/ i converted but is not working public class example : MonoBehaviour {
public Transform explosionPrefab;
void Update (){
if(Input.GetKeyDown("mouse 0")){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,ref hit)){
Quaternion rot= Quaternion.FromToRotation(Vector3.up,hit.normal);
Instantiate(explosionPrefab,hit.point,rot);
}
}
}
}
and here is the link for the non working example from the Reference look at the button in C# http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html?from=RaycastHit
ScreenPointToRay uses the physics engine to collide with objects in the world. If there is nothing to collide with, it won't work.
i know that but this script is to collide with everything i click on, in javaScript with the same method is working.. what i am doing wrong in the conversion ? and why at the References example C# isn't working too?
ok i found it u have to do like this if(Physics.Raycast(ray,out hit))
and is working perfectly i add an if st. to see if that i am hitting is the floor and now i can make a move to mouse translate :D