Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DayyanSisson · Nov 21, 2011 at 01:57 AM · rotationgunrandom.rangeaimingaccuracy

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rowdyp · Nov 21, 2011 at 02:29 AM 0
Share

are you instantiating a prefab or using raycast for your bullet?

avatar image DayyanSisson · Nov 21, 2011 at 02:44 AM 0
Share

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);
     }
avatar image sacredgeometry · Nov 21, 2011 at 03:15 AM 0
Share

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.

avatar image DayyanSisson · Nov 21, 2011 at 04:00 AM 0
Share

I realize that, but I don't know how to implement it in my script. How would I control the direction of the bullets?

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by GADefence · Nov 21, 2011 at 03:25 AM

I am assuming

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DayyanSisson · Nov 21, 2011 at 03:58 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DayyanSisson · Nov 21, 2011 at 03:56 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges