Hello peeps, i have issues writing code for pointing a gun at a Ray hit target position. Point work exept whent i piont at the sky the gun look at the world center position. How can i make it reset to a defalt position when pointing into infinity (without playcing walls all around)?
public class WeaponPointer : MonoBehaviour
{
public Camera viewCam;
public Transform aimPoint;
[SerializeField] Image crosshairs;
public Transform weanBasePos;
RaycastHit hit;
void Start()
{
viewCam = Camera.main;
weanBasePos = GameObject.Find("WeaponNormPosition").GetComponent<Transform>();
}
void FixedUpdate()
{
//Ray from cam to crosshair center
Ray ray = viewCam.ScreenPointToRay(crosshairs.gameObject.transform.position);
// Passing hit target position to guns lookAt
Physics.Raycast(ray, out hit, Mathf.Infinity);
transform.LookAt(hit.point);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
if (hit.distance > 500)
{
transform.rotation = weanBasePos.rotation;
}
}
}