- Home /
Rifle Accuracy w/ Rotation
I'm trying to make my rifle less accurate if the person is looking down a scope, and obviously more accurate when the player looks down the sights. I've been searching all over the forums and Unity Answers for a solution to this like FPS Gun Accuracy & Gun Accuracy with Random Range (which unfortunately are in JavaScript), but nothing I've tried has worked, or has gotten errors. The bullet has a script attached to it, controlling it's speed and other non-relevant information, and, it's rotation is based off the gun's shootpoint's rotation. I was thinking that I could change accuracy by changing the shootpoint's rotation very slightly ever time the player fires a bullet, making each bullet have a slightly different rotation. When the player looks down the sights, the amount the rotation changes is lowered, therefore making it more accurate. It should work, but I have no idea how to change the shootpoint's rotation. I thought the that I would have it's rotation in update, and then add a random range to it like this :
accuracyModifier = Random.Range(minAccuracy, maxAccuracy);
primarySP.rotation = Quaternion(1 + accuracyModifier, 1 + accuracyModifier, 1 + accuracyModifier);
But, that wouldn't work, because the rotation wouldn't always be 1, so I'm absolutely confused. I can't change the direction of the bullet because it's controlled by another script. Here's the script I use on the rifle (doesn't have accuracy modifiers) :
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[AddComponentMenu("Void Runners/Weapons/Guns/Assault Rifle")]
public class AssaultRifle : MonoBehaviour {
public bool on = true;
public string rifle;
public float fireRate = 0.2F;
public int clips = 5;
public int roundsPerClip = 30;
static public int clipsLeft;
static public int roundsInClip;
public GameObject round;
//public AudioClip shot;
private Transform primarySP;
private Transform gun;
private float nextFire = 0.0F;
private bool reloading = false;
private float bulletSpeed;
void Awake () {
if(!on) return;
gun = transform;
primarySP = gun.Find("PSP");
rifle = gun.name;
}
void Start () {
clipsLeft = clips;
roundsInClip = roundsPerClip;
}
void Update () {
if(!on) return;
bool hasFiredPrimary = false;
bool canFire = clipsLeft >= 0;
reloading = false;
if(clipsLeft <= 0){
clipsLeft = 0;
}
if(Input.GetMouseButton(0) && Time.time > nextFire && canFire && roundsInClip > 0){
nextFire = Time.time + fireRate;
hasFiredPrimary = true;
Instantiate(round, primarySP.position, primarySP.rotation);
//animation.Play("Firing");
//audio.PlayOneShot(shot);
}
if(hasFiredPrimary){
roundsInClip--;
}
if(roundsInClip == 0 && clipsLeft > 0){
reloading = true;
Invoke("ReloadAmmo", 3);
//animation.Play("Reload");
}
}
void ReloadAmmo () {
roundsInClip = roundsPerClip;
if(reloading){
clipsLeft --;
}
}
void OnTriggerEnter (Collider collider) {
if(collider.CompareTag(rifle)){
roundsInClip += 15;
clipsLeft += 0;
}
}
}
I wasn't sure how to implement my strategy, and since everything I tried didn't work, I posted the clean version of the script.
Edit
Okay, I made some progress regarding this. I used transform.Rotate, to rotate to shootpoint, but the problem is that it rotates, but then it doesn't go back to it's original rotation. How should I do that? Here's the script modifications :
if(Input.GetMouseButton(0) && Time.time > nextFire && canFire){
nextFire = Time.time + fireRate;
hasFiredPrimary = true;
Instantiate(round, primarySP.position, primarySP.rotation);
AccuracyModifier();
animation.Play("Firing");
audio.PlayOneShot(shot);
}
And later on...
void AccuracyModifier () {
float x = Random.Range(minAccuracy, maxAccuracy);
float y = Random.Range(minAccuracy, maxAccuracy);
float z = Random.Range(minAccuracy, maxAccuracy);
primarySP.Rotate(x,y,z);
}
I have no idea how to set it back to it's original rotation. I can't set it's original rotation using the Start void because it takes the starting rotation. I know that in-between each shot, I want the rotation to go back the way it was based on how it would be before AccuracyModifier() took place.
are you instantiating a prefab or using raycast for your bullet?
The script is there for a reason. But I'm instantiating a prefab. Here's the firing code :
if(Input.Get$$anonymous$$ouseButton(0) && Time.time > nextFire && canFire && roundsInClip > 0){
nextFire = Time.time + fireRate;
hasFiredPrimary = true;
Instantiate(round, primarySP.position, primarySP.rotation);
animation.Play("Firing");
audio.PlayOneShot(shot);
}
whats the reason? Because i would strongly recommend against using colliders for anything but really slow weapons. Either way you could use a random range to control the direction of fire.
I realize that, but I don't know how to implement it in my script. How would I control the direction of the bullets?
Answer by DayyanSisson · Nov 22, 2011 at 08:03 PM
I had this answered on the forums on this thread. I will put the full script later, possibly on the wiki.
Answer by GADefence · Nov 21, 2011 at 03:25 AM
I am assuming
This is the beginning of your answer, put you didn't finish it. Just delete it, because it says there are 2 answers when there really only is 1.
Answer by GADefence · Nov 21, 2011 at 03:25 AM
Assuming 1 is 100% accuracy.
When someone presses the key (assuming right mouse button) to bring up the iron sights,
RightMousePress() { SightModifier = .75; }
When they let it go, and are not looking down the iron sights.
RightMouseRelease() { SightModifier = .25; }
Then, add this to your code. accuracyModifier = Random.Range(minAccuracy SighModifier, maxAccuracy SightModifier);
Play with these numbers as you want. A 100% accurate gun would be 75% accurate in iron sights that way, and 25% accurate not in iron sights.
I know how to do that, but that code wouldn't actually make a difference in the accuracy. The bullets would all still shoot straight with no accuracy change. So even though the numbers claim the accuracy is less, the rotation of each bullet stays the same.
Your answer
![](https://koobas.hobune.stream/wayback/20220613050940im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Gun Random Rotation 0 Answers
Why are my guns shooting lower when my character moves forward? 1 Answer
Help with gun accuracy in degrees. 3 Answers
Aiming down sights 4 Answers
Mouse Aiming Causing Rotation Jitter 3 Answers