- Home /
How can I decrease accuracy when the gun is fired?
Hi everyone!
I would like someone to tell me how to make the accuracy of the gun decrease when it is fired. The part that fires the raycast is "var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));" and "if (Physics.Raycast (ray, hit, 500))". The rest just instantiates a bullet effect. I know that Haim96 answered a similar script, but I didn't make it clear enough to him. Thanks, Stormy102
#pragma strict
var Effect : Transform;
var TheDammage = 100;
var gunshotSound : AudioClip;
var reloadGrenade : AudioClip;
var ammoCount : int = 10;
var totalAmmo : int = 120;
var reloadTime : int = 3;
var reloadAmount : int = 10;
var ammo : GUIText;
var gunName : GUIText;
var fireRate : float = 3.0;
var bulletHole : Material;
var force = 10;
private var nextFire : float =0.0;
function Update ()
{
Fire ();
Reload();
OnGUI();
}
function Fire () {
if (ammoCount > 0) {
if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
InstantiateGrenade();
ammoCount -=1;
}
}
}
//---------------------------------------------------------------------------------
function InstantiateGrenade () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
{
AudioSource.PlayClipAtPoint(gunshotSound, transform.position, 1);
if (Physics.Raycast (ray, hit, 500))
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(bulletHole, hit.point, hitRotation);
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
//---------------------------------------------------------------------------------
function Reload () {
if(ammoCount < 1 && totalAmmo > 10) { //if we have less than 1 bullet then we can reload
if(Input.GetKeyDown("r")) {
AudioSource.PlayClipAtPoint(reloadGrenade, transform.position, 1);
yield WaitForSeconds(reloadTime);
ammoCount += reloadAmount;
totalAmmo -= reloadAmount;
}
}
}
//---------------------------------------------------------------------------------
function OnGUI () {
ammo.text = "Ammo: " + ammoCount + "/" + totalAmmo.ToString();
gunName.text = "Lee-Enfield Mark III";
}
This question is very much like your last question, and it seems you are just asking for others to custom edit code for you. I'm very temped to close this ....
Go and watch the Turret Tutorial set in CGCookies. Awsome site for learning unity and I think video 1 has your answer in the turret game tutorial series. GL
Answer by Ermarrero · Apr 05, 2014 at 05:00 PM
No need to call onGUI() from inside the update function , it already loops on its own.
Answer by Cynikal · Apr 05, 2014 at 10:52 PM
Make a float, call it RandomAccuracy.
The longer you hold down the mouse button (shoot), it will ADD to that value.
When you're raycasting out your hit, add the desired location, to a Random(0, RandomAccuracy) to each of your Vertical/Horizontal axis.
Add a timer or some other function, that decreases RandomAccuracy while you're not holding down the shoot key.
That way, when you shoot once... it should be dead on. If you hold it down, it'd start to go haywire.
Your answer
Follow this Question
Related Questions
musket gun script is not working 3 Answers
Need coding help with fps 0 Answers
How do I make a gun? 1 Answer
Controlled Automatic Fire Rate (RayCasting) 2 Answers
Clicking on an Object to Make it the Variable Target 1 Answer