- Home /
Accuracy not working properly
Here is my raycast gun accuracy script:
Vector3 direction = transform.rotation.eulerAngles;
direction.y += Random.Range(-accuracy, accuracy);
ray.origin = transform.position;
ray.direction = direction;
What is wrong with it? I cannot wrap my head around it!
it's not casting in the correct direction? raycast not hitting anything? i think it'll help if you elaborate on what is wrong.
I have tried almost every possible answer on almost every possible question about this topic. It still does not work. I have been working on this for a day. Accuracy is flying around the place. I am working on a 2.5D game and I need inaccuracy for only one axis, but nothing works!
Someone please help me out.
Answer by Loken_01 · Jan 03, 2015 at 03:38 AM
Hey Calmcarrots!
I made two versions of a function that can use inaccuracy ranges, either for custom vectors or a transform!
Here you go:
//Calculate with custom vectors
Vector3 CalcAccuracy(Vector3 Forward, Vector3 Right, Vector3 Up, float pAccuracy)
{
//Get the error range
float RandAcc = Random.Range(-pAccuracy, pAccuracy)/10;
//Calc left and right error range
Vector3 newDir = Forward + Right * RandAcc;
//Optional - Recalc error range for up and down
RandAcc = Random.Range(-pAccuracy, pAccuracy)/10;
//Calc up and down error range
newDir = newDir + Up * RandAcc;
return newDir;
}
//Calculate with a transform
Vector3 CalcAccuracy(Transform pTrans, float pAccuracy)
{
//Get the error range
float RandAcc = Random.Range(-pAccuracy, pAccuracy)/10;
//Calc left and right error range
Vector3 newDir = pTrans.forward + pTrans.right * RandAcc;
//Optional - Recalc error range for up and down
RandAcc = Random.Range(-pAccuracy, pAccuracy)/10;
//Calc up and down error range
newDir = newDir + pTrans.up * RandAcc;
return newDir;
}
You can remove the second half of the functions if you only want a left to right update.
Hope this help!
-Peace
Your answer
Follow this Question
Related Questions
How to Implement First Shot Accuracy? 2 Answers
Problem in Shooting Accuracy 0 Answers
accuracy problem with ray cast 2 Answers
How make Raycast Aim to Crosshair? 1 Answer
How to obtain third person accuracy 0 Answers