Help with angles and quaternion please!

Hello all.

I’m making a 2D top down shooter. I have enemies that shoot at the player and I’m fine with coding this. For the record I’m using this to code the instantiation of the enemy weapons fire.

Vector3 FirePos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 FireDir = Input.mousePosition - FirePos;
float angle = Mathf.Atan2(FireDir.y, FireDir.x) * Mathf.Rad2Deg;

Laser_X_Instance = Instantiate(Laser_X, transform.position, Quaternion.AngleAxis(angle, Vector3.forward)) asRigidbody2D;

What I’m having trouble with is making them inaccurate.

How do you code the instantiate so that rather than have perfect aim, the enemies will shoot to a random angle that is within ±0.5 degrees of perfect aim? I have looked through the tutorials and forums but cannot find how to do this and I have to admit that I struggle with quaternion.

Can anybody please help me with this?

Just add Random.Range(-0.5f, 0.5f) to angle.

1 Like

angle += Random.Range(-0.5f, 0.5f)?

Is it really that easy??

Just tried it. Thank you so very much! Everything I was trying was so very much more complicated than that!