My problem is that my raycasts for a gun are most commonly projecting from a little above the camera making shooting very in accurate since the rays are meant to hit centre of screen. Also, a very small ratio of rays are projecting accurately but i do not understand why some rays are projecting from above the camera instead of central. I don’t have any other camera in scene yet either apart from the camera in parent of the gun. This could be my error or a bug in unity. This is the code I used to visualise the rays in a separate script:
using UnityEngine;
public class RayViewer : MonoBehaviour {
public float range=100;
private Camera fpsCam;
// Use this for initialization
void Start () {
fpsCam = GetComponentInParent<Camera> ();
}
// Update is called once per frame
void Update () {
Vector3 lineOrigin = fpsCam.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, 0));
Debug.DrawRay (lineOrigin, fpsCam.transform.forward * range, Color.green);
}
}