- Home /
Random /Organic/ Weapon Inaccuracy
Hey guys, here's an interesting one.
I want to simulate the way the orientation of a firearm "creeps" while you hold it in your hands. That is, I need some manner a generating a sort of smooth random x and y rotation over time.
I've researched bezier curves, perlin noise, noise curves, and all sorts of other things but I can't seem to get a bead on something that would be appropriate for this situation.
My best guess at the moment is using some kind of randomized polar coordinate function, where the radius change between -1 and 1 while the angle randomly rotates around, but I can see this leading to some weird movement.
Any ideas how something like this could be accomplished?
Cheers,
Wes
Answer by SergeantBiscuits · Apr 11, 2013 at 08:06 AM
Well, I managed to make something passable. This randomly deflects a horizontal/vertical angle "point of aim" by up to 'maxCreepAngle' smoothly over time.
This goes in Update() or whatever:
var maxCreepAngle : float = 1.0;
var creepMinRapidity : float = 2.0;
var creepRelaxedness : float = 3.0;
viewCreepXCooldown -= Time.deltaTime;
if( viewCreepXCooldown <= 0 )
{
viewCreepXCooldown = Random.Range( 0.0, creepMinRapidity );
viewCreepXTarget = Random.Range( -maxCreepAngle, maxCreepAngle );
}
viewCreepX = Mathf.SmoothDamp( viewCreepX, viewCreepXTarget, viewCreepXVel, creepRelaxedness );
viewCreepYCooldown -= Time.deltaTime;
if( viewCreepYCooldown <= 0 )
{
viewCreepYCooldown = Random.Range( 0.0, creepMinRapidity );
viewCreepYTarget = Random.Range( -maxCreepAngle, maxCreepAngle );
}
viewCreepY = Mathf.SmoothDamp( viewCreepY, viewCreepYTarget, viewCreepYVel, creepRelaxedness );
Just add viewCreepX and viewCreepY to your camera/weapon/whatever angles and you're good to go.
Maybe someone will find this useful for an FPS or similar.
Peace out.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092308im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move navmesh agent in random direction within range 1 Answer
Cannonball physics? 1 Answer
how to get a correct rotation 2 Answers
Raycast across angle? 3 Answers
2D TOP DOWN - W A S D as direction 2 Answers