Skip to main content

Adding Inaccuracy To Shooting

Let's say you have a shooter game and its just plain to hard. Your Enemies are perfect shots and can hit you straight on every time. Making the game a little more fair would be nice, and one way doing so is by dumbing down your enemies AI a bit, and making them a less accurate with aiming.

My method of aiming and inaccuracy is done like this. I use this for cannons, turrets, etc.

-------------------------------------------------------------------------------------------------------------
var target = GameObject.Find("My Target");


var randPos = Random.Range(0.9, 1.1);


var tarPos = Vector3(target.transform.position.x * randPos, target.transform.position.y * randPos, target.transform.position.z * randPos);


var rotate = Quaternion.LookRotation(tarPos - transform.position);


function Update(){
         transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime);
}
-------------------------------------------------------------------------------------------------------------
Explanation:

var target = GameObject.Find("My Target");
I use this simple way of obtaining a target for this example. Finds a game object in the scene named "My Target". Read more here.

var randPos = Random.Range(0.9, 1.1);
When ever the variable is called, It will produce a random float number between the minimum and maximum numbers you set. Read more from original content here.

var tarPos = Vector3(target.transform.position.x * randPos, target.transform.position.y * randPos, target.transform.position.z * randPos);
Gets the targets vector 3 coordinates but multiplies each position by the random range. Doing this returns an inaccurate position.

var rotate = Quaternion.LookRotation(tarPos - transform.position);
Returns an inaccurate rotation due to multiplying the original target position with the random range. Read more from original content here.

function Update()
Called every frame. Meaning it will keep a constant rotation towards it's target. This also means the random position variable is being called every frame, which means it will be constantly changing. As will the rotation.

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime);
Sets the models rotation from the currents rotation to the rotate position variable. This is done over Time.deltaTime. Read more here.

If you have any questions or suggestions please leave them in the comments below. Some feedback would be nice. If I need to be more specific or clear please also say.

Comments

Popular posts from this blog

How To Make a Hellish Looking Sky Box

I came across this problem while constructing my scene of Hell in a little project I've been working on, and could not find a reasonable sky box on the web for what I want. Maybe I was not looking hard enough, but I ended up making nice substitute. If you think the sky box looks familiar, then your right. The Sky box I'm using is already packaged with Unity3D! To import the sky boxes Unity has made for you,  simply go to Assets>Import Package>Skyboxes.  The sky boxes will appear in your projects tab under a folder named "Standard Assets". To make this sky box, first you must find the folder containing all the sky box materials and open it up. In it will be a list of sky boxes for your disposal. To get this skybox, I decided to tweak the "StarryNight Skybox" (But the "MoonShine Skybox" looks pretty cool also!).  Select the sky box and view it under the inspector tab. Underneath the properties there will be a tint color variable allowin

Making A Laser Pointer

Want a frieking lazer pointer? BOOM. Attach a Line Renderer component to the object you have the script below attached to. (Component> Miscellaneous>Line Renderer) Code: function Update () { var lineRenderer : LineRenderer = GetComponent(LineRenderer); lineRenderer.useWorldSpace = false; lineRenderer.SetVertexCount(2); var hit : RaycastHit; Physics.Raycast(transform.position,transform.forward,hit); if(hit.collider){ lineRenderer.SetPosition(1,Vector3(0,0,hit.distance)); } else{ lineRenderer.SetPosition(1,Vector3(0,0,5000)); } } @script RequireComponent(LineRenderer) Thank  3dDude for the script. Original Source here .

How To Make A Gun Shot Sound (SFX On Unity 3D)

When it comes to audio in Unity, there are four components: Audio Clip , Audio Source , Audio Listener , and Audio Re-verb Zone . Audio Clips are the actual audio file imported into your game. Unity supports file formats: .aif, .wav, .mp3, and .ogg. When imported, you can compress them greatly, with the price of loosing some quality. You can do this by first selecting the audio clip, view it in the inspector. Under the Audio Importer component, you can switch the audio format from Native to the audio clip, to a compressed format applied by Unity. You can change how compressed the file is by dragging the bar at the bottom, then hitting apply. You can get plenty of free good SFX from a site called  freesound.org . All you have to do is create an account for free , and download all the sounds you want. I found a nice gun shot sound here . Simply download and load into your Project. Audio Source actually plays the audio clip in your scene. They are an component, so it mu