How do you give projectiles inaccuracy?

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
User avatar
Lt.blam
Posts: 87
Joined: Thu Jun 16, 2016 9:01 pm
Location: https://www.youtube.com/watch?v=fq3abPnEEGE

How do you give projectiles inaccuracy?

Post by Lt.blam »

As the title reads. Is there a way to add projectile inaccuracy?
Accensus
Posts: 2383
Joined: Thu Feb 11, 2016 9:59 am

Re: How do you give projectiles inaccuracy?

Post by Accensus »

In A_FireCustomMissile add random(-x, y) for both angle and pitch where -x and y can be the same numbers, which is usually used most. The negative number ensures the projectile doesn't just fly in one single direction everytime, say, all projectiles go above the crosshair, for example.

Example:

Code: Select all

A_FireCustomMissile("TestMissile", random(-25, 25), 1, 0, 0, 0, 0, random(-25, 25))
You can also use frandom(-25.0, 25.0), though I don't know if there is much of a difference. Do take note that the area the projectiles will hit is a square. Using frandom may make the square less perfect, though that's just an observation of mine and could be just an illusion.
Last edited by Accensus on Fri Aug 26, 2016 1:59 pm, edited 1 time in total.
User avatar
Lt.blam
Posts: 87
Joined: Thu Jun 16, 2016 9:01 pm
Location: https://www.youtube.com/watch?v=fq3abPnEEGE

Re: How do you give projectiles inaccuracy?

Post by Lt.blam »

Thanks!
Accensus
Posts: 2383
Joined: Thu Feb 11, 2016 9:59 am

Re: How do you give projectiles inaccuracy?

Post by Accensus »

No problemo, pal. :)
User avatar
wildweasel
Posts: 21706
Joined: Tue Jul 15, 2003 7:33 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): A lot of them
Graphics Processor: Not Listed
Contact:

Re: How do you give projectiles inaccuracy?

Post by wildweasel »

Lud wrote:You can also use frandom(-25.0, 25.0), though I don't know if there is much of a difference. Do take note that the area the projectiles will hit is a square. Using frandom may make the square less perfect, though that's just an observation of mine and could be just an illusion.
There is a way to "math out" the spread so that it's circular, but is a bit of a pain. Where frandom is useful, though, is to enhance the precision of randomness, since it does not need to be in whole numbers. This means that, for example, shotguns that fire a lot of pellets will not appear to be firing on a grid.
Accensus
Posts: 2383
Joined: Thu Feb 11, 2016 9:59 am

Re: How do you give projectiles inaccuracy?

Post by Accensus »

That explains a whole lot. Thanks, ww!
User avatar
Rachael
Posts: 13575
Joined: Tue Jan 13, 2004 1:31 pm
Preferred Pronouns: She/Her
Contact:

Re: How do you give projectiles inaccuracy?

Post by Rachael »

If you want the projectile inaccuracy to appear less square, you can multiply 2 random results. Example:

Code: Select all

A_FireCustomMissile("TestMissile", random(-25, 25)*random(-25, 25)/25, 1, 0, 0, 0, 0, random(-25, 25)*random(-25, 25)/25)
You will have to adjust the math a bit since now the possible outcome can be much bigger, but at the same time what it will do is make it appear much more likely that the result appears closer to the crosshair. Because of this, and because of two checks (2 axes) it is far more likely that the result will appear on a line rather than a corner, or somewhere near the center.

Corner shots will still happen, but their incidence will be less often. You probably won't even notice it unless you enable decals and stare at the patterns on the wall.
User avatar
NeuralStunner
 
 
Posts: 12326
Joined: Tue Jul 21, 2009 12:04 pm
Preferred Pronouns: He/Him
Graphics Processor: nVidia with Vulkan support
Location: capital N, capital S, no space
Contact:

Re: How do you give projectiles inaccuracy?

Post by NeuralStunner »

You could simplify that by quite a bit, having the desired angle spread in the first frandom() and (-1,1) in the second:

Code: Select all

FRandom(-5,5)*FRandom (-1,1)
Though I've gotten good enough results from adding two frandom()s at half the desired angle apiece. On the surface you wouldn't expect it to make any difference, but the outcome tends to look less square to me. (Statistically, adding two random numbers skews the results toward the average.)
User avatar
Rachael
Posts: 13575
Joined: Tue Jan 13, 2004 1:31 pm
Preferred Pronouns: She/Her
Contact:

Re: How do you give projectiles inaccuracy?

Post by Rachael »

I never knew about adding randoms together, so that's a good idea, too.
Locked

Return to “Editing (Archive)”