How do I get the crosshair to line up with the results of a raycast when hit something? I looked at the FPSDemo I have no way how they get perfect accuracy.
The Ray class has a method called GetPoint which gets a point a certain distance along the ray. With a ray generated from the camera (eg, with ScreenPointToRay), all points along the ray correspond to the same screen position. Use Ray.GetPoint with the ray you are using for the raycast to get a location for the crosshair object.
Is this how I would do it?
function HandGun () {
var hit : RaycastHit;
var accuracy : Ray;
var direction = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast (transform.position, direction, hit, range))
{
if (bulletEffects)
bulletEffects.transform.position = hit.point;
bulletEffects.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
bulletEffects.Emit();
print( accuracy.GetPoint (10));
}
if (hit.rigidbody)
{
var contacts = hit.rigidbody;
}
gunSound.Play();
}