Question by
shamblinwithshotguns · May 14, 2018 at 03:35 PM ·
c#raycastquaternion
(Raycasts) C# Why am I shooting myself?
This is the offending function. I thought it was working correctly, but sometimes the weapon will hit the Player even though I have a layer mask set up.
public float SpreadAngle;
void ShootGun()
{
RaycastHit hit;
// Finding the fire angle
Quaternion fireRotation = Quaternion.Euler(new Vector3(GameObject.Find("Camera").transform.eulerAngles.x, transform.eulerAngles.y, 0));
Quaternion randomRotation = Random.rotation;
fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0.0f, SpreadAngle));
// If the ray hits something
if (Physics.Raycast(transform.position, fireRotation * Vector3.forward, out hit, mask))
{
Debug.DrawLine(transform.position, hit.point);
Debug.Log("Raycast from " + transform.position + fireRotation + " hit " + hit.transform.name + " at " + hit.point);
if (hit.transform.tag == "Enemy")
{
hit.transform.GetComponent<Scr_Health>()._Damage(Damage);
}
GameObject hitmarker = Instantiate(HitMarker, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(hitmarker, 1);
}
}
Here are some of the debug messages I've been getting. It appears to only be a problem with a specific angle.
Comment
Your answer
Follow this Question
Related Questions
Inconsistent Jump Heights 1 Answer
How to set a max rotation angle 1 Answer
Clamp a rotation in unity 0 Answers
How do I smoothly rotate the GameObejcts with negative 90 degrees 0 Answers