- Home /
 
Spawning object by using ray
This is the code :
 var ray = CameraUI.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
     if(Physics.Raycast(ray,hit)){
     //Debug.Log("Now I see a cube");
     if(hit.collider.tag == "RayEnemy" ){
     Debug.Log("I see a unit");
      if(Input.GetMouseButtonDown(0))Instantiate (particle, hit.point, transform.rotation);
 
               The problem is that the particle instance is spawned somewhere related to the detected collider but if I move the camera I can see that in fact the particle instance is in mid air somewhere between collider and camera or around collider.
How can I spawn the particles (a lightning strike spell) right under collider position (on the ground under my unit because the collider is centered on the unit).
THanks
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by robertbu · May 31, 2013 at 05:40 AM
Try this (untested):
 if(Input.GetMouseButtonDown(0)) {
     var bounds = hit.collider.bounds;
     var v3 = bounds.center;
     v3.y -= bounds.extents.y;
     Instantiate (particle, v3, transform.rotation);
 
 }
 
              Quick and painless. Just how my girlfriend likes it. (Wut ?!)
Seriously, I'm super happy because it works how I imagined it. Thank you !
Your answer