- Home /
 
 
               Question by 
               cidmodder · Aug 11, 2011 at 02:52 AM · 
                javascriptmouse positionbeam  
              
 
              Mouse position help
Hi all. I'm trying to get a beam to go from an flying ship to the ground where the mouse is clicking. However I'm having a hard time getting the beam to go from the ship. Its a top down view so the camera is not attached to the ship so I have a problem with doing raycasts from the point of view of the ship. heres what I've been using
 if(Input.GetMouseButtonDown(0)){
 var mousePo = camera.ScreenToWorldPoint (Vector3 (Input.mousePosition.x, Input.mousePosition.y,camera.nearClipPlane));
 var ray = camera.ScreenPointToRay(mousePo);
 Instantiate (particle, mousePo, Quaternion.identity);
 }
 
               any idea how I can make a beam go from the ship down to the ground where the mouse is clicking? Thanks in advance!
               Comment
              
 
               
              Answer by roamcel · Aug 11, 2011 at 11:40 AM
I believe your solution is better considered in two parts:
 1- raycast from camera to ground
 2- ray from any transform to raycast hitpoint
 
               in particular, to solve the first need:
    private Plane drivingplane = new Plane(Vector3.up, Vector3.zero);...
 
    void Update () {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         float ent = 1.0f;
 
         if (drivingplane.Raycast(ray,out ent))
         {
             Vector3 hitPoint = ray.GetPoint(ent);
             Debug.DrawRay (ray.origin, ray.direction * ent, Color.green);
         }
         else
             Debug.DrawRay (ray.origin, ray.direction * 10, Color.red);
     }
 
               consequently, to solve point 2
 Debug.DrawRay (transform.position, hitpoint);
 
              Your answer