- Home /
How to give your enemy gun inaccuracy
How can I create a gun that will not shoot 100% accurate in the direction of a raycast? For example, to give it a little bit of an inaccuracy so that the Enemy AI will not have 100% shooting accuracy for every shot. I've figured out my own way but I'd like to see how others handle it.
Answer by justinl · Sep 22, 2012 at 03:08 AM
Here is my implementation. How have others done it? The main purpose of this question was to share my technique because I was not able to find any resources about how to do it but I thought it was something most people would find useful
//Takes a vector and returns a new vector that is slightly variated in direction
//0 = 100% accurate and the larger the number, the less accurate
//last 2 params are optional and will draw both the original vector and the new varied vector
public static Vector3 VectorSpread(Vector3 origVector, int accuracy, bool showDebug = false, Vector3 debugPosition = default(Vector3)){
float myIntx = (float)Random.Range(-accuracy,accuracy)/1000;
float myInty = (float)Random.Range(-accuracy,accuracy)/1000;
float myIntz = (float)Random.Range(-accuracy,accuracy)/1000;
Vector3 newVector = new Vector3(origVector.x + myIntx, origVector.y + myInty, origVector.z + myIntz);
if(showDebug){
Debug.DrawRay(debugPosition, origVector * 1000F, Color.cyan);
Debug.DrawRay(debugPosition, newVector * 1000F, Color.red);
}
return newVector;
}
Your answer
Follow this Question
Related Questions
How to Implement First Shot Accuracy? 2 Answers
How do I create bullet hole for my gun? 0 Answers
Creating a Ray Gun 1 Answer
Controlled Automatic Fire Rate (RayCasting) 2 Answers
Raycast Targeting Reticle. 1 Answer