Apologies if this is the wrong forum. I’m a teacher who typically works with sound and audio but I’m attempting to build a simple flight-simulation environment to facilitate some remote learning exercises.
My issue is that a raycast will occasionally not be triggered due to the orientation of the plane. I am wondering if it is because it’s rotation is being reduced to zero when it is pointing in a number of directions. The rotation and positioning of the ray’s beginning and end is perfectly fine and it is reacting as expected when it reaches a target. Throttle speed has no effect on the issue.
This isn’t my typical watering hole so any advice would be appreciated!
void Update()
{
if (Input.GetButton("Fire1"))
{
if (Time.time > m_shootRateTimeStamp)
{
shootRay();
m_shootRateTimeStamp = Time.time + shootRate;
}
}
}
void shootRay()
{
Ray ray = new Ray (transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
GameObject laser =
GameObject.Instantiate(m_shotPrefab, transform.position, transform.rotation) as GameObject;
laser.GetComponent<ShotBehavior>().setTarget(hit.point);
GameObject.Destroy(laser, 2f);
}