Search Unity

Raycast from Camera sometimes appearing above camera

Discussion in 'Editor & General Support' started by HopelessHyena, May 26, 2015.

  1. HopelessHyena

    HopelessHyena

    Joined:
    May 26, 2015
    Posts:
    18
    Hello Everyone,

    I am creating a simple FPS game, and have my character set up to be able to move around.

    When I click the mouse, the character is supposed to shoot (using Raycasting) from the centre of the camera (i.e. the camera location) in the forwards direction relative to the camera.

    However, although this works correctly sometimes, other times the ray will be cast from slightly above the camera (about 1 or 2 units by the looks of it).

    It does something like this: (the triangle is the camera frustum, and the two lines represent where the ray casts take place).


    I'm really not sure why this would happen. The camera is attached as a child to an empty. The empty has a capsule collider. My code for shooting the ray is as follows:

    Code (CSharp):
    1.     void shootBullet()
    2.     {
    3.         Ray shootRay = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    4.         RaycastHit hitInfo;
    5.  
    6.         Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward*10, Color.green, 5, false);
    7.  
    8.         if(Physics.Raycast(shootRay, out hitInfo, 300f))
    9.         {
    10.             Vector3 hitPoint = hitInfo.point;
    11.             if(concrete_impact_particles != null){Instantiate(concrete_impact_particles, hitPoint, Quaternion.FromToRotation(Vector3.forward, hitInfo.normal));}
    12.         }
    13.     }
    The only time the camera has anything done to it is in the rotation script:
    Code (CSharp):
    1.     void TurningManagement()
    2.     {
    3.         //Up down rotation is applied to camera
    4.         float rotLeftRight = Input.GetAxis ("Mouse X")*rotSpeed;
    5.         float rotYUpDown = Input.GetAxis ("Mouse Y")*rotSpeed;
    6.         transform.Rotate (0, rotLeftRight, 0);
    7.  
    8.         verticalRotation -= (rotYUpDown);
    9.         float desiredUpDownRot = Mathf.Clamp (verticalRotation, -maxUpDownRotRange, maxUpDownRotRange);
    10.  
    11.         //If the rotation IS greater than the max range, set the rotation to the border value so prevent over-rotating.
    12.         if((verticalRotation > maxUpDownRotRange)){verticalRotation = maxUpDownRotRange;}
    13.         else if((verticalRotation < -maxUpDownRotRange)){verticalRotation = -maxUpDownRotRange;}
    14.  
    15.         cam.transform.localRotation = Quaternion.Euler (desiredUpDownRot, 0, 0);
    16.  
    17.     }
    The lower lines are the correctly cast rays, but for some reason, some (most) rays appear above the camera
     
    Last edited: May 26, 2015
  2. HopelessHyena

    HopelessHyena

    Joined:
    May 26, 2015
    Posts:
    18
    Update: If I set the players Capsule Collider/Rigid Body to kinematic, it works more or less as it should do.