I want to add inaccuracy to fired bullets.
I’ve been using the typical method that adds a randomized vector3 by the velocity vector3 of the bullet. This is fine for a quick and dirty solution, but falls short in a number of areas.
-
As velocity increases, so too does the accuracy. This is bad.
-
The “gun accuracy” value gives no good face value meaning without extensive trial and error.
-
The bullet doesn’t actually face in the direction of movement.
So, I’m trying to construct an inaccuracy system that allows me to define gun accuracy in a simple degree notation.
e.g. If a gun has an accuracy value of 5, then the shot will fire up to 5 degrees off from the the barrel line. Or within a cone with radius of 5.
var accuracy : float;
var error : Vector2 = Random.insideUnitCircle * accuracy;
var errorRotation : Quaternion = Quaternion.Euler(error.x, error.y, 0);
var bullet : GameObject = Instantiate( prefab, transform.position,
transform.rotation * errorRotation);
Random.insideUnitCircle gives me a random value of -1 to 1 in each direction (but only in a circle area)
So multiplying by the accuracy value should give me a random point in a circle with accuracy as the radius. This will represent the degrees of rotation from straight.
Quaternion.Euler() should then convert those degree values into a rotation.
So, multiplying the gun rotation by the errorRotation should give me something within that cone I’m looking for, right?
Except it doesn’t.
Even with accuracy of 1, bullets fly everywhere, not in a 1 degree cone.
With an accuracy of 0, the bullets fly straight with no error.
What am I doing wrong here?
Vizas
2
For the bullet facing direction, you can use the LookAt method from the bullet’s transform.
You can set it as the following to achieve what you want.
var accuracy : float;
var error : Vector2 = Random.insideUnitCircle * accuracy;
var bullet : GameObject = Instantiate( prefab, transform.position, Quaternion.identity);
bullet.transform.LookAt(transform.forward + (transform.right * error.x) + (transform.up * error.y));
Desprez
3
Well, it looks like my original code is correct.
I believe something was changing the result later. In addition, Unity was doing some strange things that disappeared when I restarted it.
For a much faster method, that is also keeps working at high angles (up to 360 degrees), while also being perfectly uniform, you can use this:
public static Vector3 GetRandomUniformVectorInCone(float radius)
{
//http://math.stackexchange.com/questions/56784/generate-a-random-direction-within-a-cone
//The 2 - sphere is unique in that slices of equal height have equal surface area
//That is, to sample points on the unit sphere uniformly, you can sample z uniformly on[−1, 1] and ϕ uniformly on[0, 2π).
//If your cone were centred around the north pole, the angle θ would define a minimal z coordinate cosθ,
//and you could sample z uniformly on[cosθ, 1] and ϕ uniformly on[0, 2π) to obtain the vector
//(sqrt(1 - z^2) * cosϕ, sqrt(1 - z^2) * sinϕ, z)
float radradius = radius * Mathf.PI / 360;
float z = Random.Range(Mathf.Cos(radradius), 1);
float t = Random.Range(0, Mathf.PI * 2);
return new Vector3(Mathf.Sqrt(1 - z * z) * Mathf.Cos(t), Mathf.Sqrt(1 - z * z) * Mathf.Sin(t), z);
}
The source is, as stated, this problem: geometry - Generate a random direction within a cone - Mathematics Stack Exchange
Simply multiply the resulting vector by your direction quaternion, and everything will work fine.