- Home /
Gun accuracy
Hello everyone
I am making a FPS prototype and it is going very well, but I can´t rap my brain around gun accuracy and showing a moving crosshair on a Guitext. My gun uses raycast to fire, but it is shooting in a strait line using transform.forward. I all ready looked at DastardlyBanana´s FPS package, but lets face it, it is just a little too complicated for me.
So can you give me some leads on how to make a spray effect using raycast, and a moving crosshair showing the guns accuracy on a Guitext?
Thank you.
Answer by PrimeDerektive · Jun 30, 2011 at 08:32 PM
Spread on a raycast is pretty simple. Just add a random factor to the x y and z of your direction vector before you pass it to the raycast. Something like this:
//declare a float value to control the spread factor. .02 is a good default
public var spreadFactor : float = 0.02;
then when you're doing the raycast, instead of passing it transform.forward, store it in a variable before hand and add the spread.
var direction : Vector3 = transform.forward;
direction.x += Random.Range(-spreadFactor, spreadFactor);
direction.y += Random.Range(-spreadFactor, spreadFactor);
direction.z += Random.Range(-spreadFactor, spreadFactor);
if (Physics.Raycast (transform.position, direction, hit, distance)){
That is awesome and it works. I can´t believe it is "that" simple. I can´t and will not ask for more, but in any case do you know how to show it on a GuiText(crosshair)? :)
That might be odd to view, because if you were constantly calculating your angle and then displaying it on the crosshair, you would have a barely visible GUI that constantly flickers around the center of the screen with no precision or order. It might be better to just add a line or trail(depending on how you are making these bullets), and a bullet hole where it hits. Besides, the idea of gun accuracy is to make a gun a bit unreliable, so that better accuracy is, well, better. If you already know where your bullet is going to go, a shotgun would have effectively the same accuracy at a distance a sniper rifle, because the player can just adjust.
Answer by JosephLim · May 10, 2018 at 06:36 AM
Hi can i ask for the C# part for this?
It is similar to the JS version , just replace how variables are declared :
public float spreadFactor = 0.02f;
Vector3 direction = transform.forward;
direction.x += Random.Range(-spreadFactor, spreadFactor);
direction.y += Random.Range(-spreadFactor, spreadFactor);
direction.z += Random.Range(-spreadFactor, spreadFactor);
if (Physics.Raycast (transform.position, direction, out hit, range, canHitLayer$$anonymous$$ask))
{
//Shooting Code
}
*Note : I havent tested this, im just writing it how i think it should be, and i might be late to answer this ..... so xD
Your answer
Follow this Question
Related Questions
RaycastHit info works using print() but not GUIText? 1 Answer
How to implement gun accuracy/spread with raycast 1 Answer
Accuracy not working properly 1 Answer
How to give your enemy gun inaccuracy 1 Answer
More precise Vector3 2 Answers