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
1
Question by hugemaggot · Dec 05, 2010 at 05:30 PM · fpsbullets

spraying bullets

ive been working on an fps game in unity for some time now but i dont know how to spray my bullets instead of the bullets going straight forward which isnt very realistic.

can anyone help me with?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by Rennat · Dec 05, 2010 at 05:58 PM

You'll need to use Random. I write code better than I write sentences:

public var maxDivergence : float = 5; // The amount in degrees the bullet can be "off" center

function Shoot() {

 // start with a perfect shot
 var divergence : Vector3 = Vector3.zero;

 // then we want to randomize the rotation around the X axis
 divergence.x = (1 - 2 * Random.value) * maxDivergence;
 // and the rotation around the Y axis
 divergence.y = (1 - 2 * Random.value) * maxDivergence;
 /* Random.value only gives you a positive float between 0 and 1
  * (1 - 2 * Random.value) makes that between -1 and 1
  */

 // instantiate the bullet
 var bullet : Transform = (Transform) Instantiate(bulletPrefab, transform.position, transform.rotation);

 // now we'll apply the divergence
 bullet.Rotate(divergence);

}

It would be more efficient to make the Quaternion rotation before instantiating the prefab but Quaternions scare me. I'm making this a Wiki maybe someone will add it in.


Also check out the answers on this question http://answers.unity3d.com/questions/5602/fps-gun-accuracy-bullet-tracers

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 JPhilipp · Dec 07, 2018 at 07:18 PM 0
Share

Thanks, I've tried this, but it gives me a rectangular spread, ins$$anonymous$$d of the expected circular one. That is, if I were too shoot 10,000 bullets with this, the damage would look like a square, not a circle.

avatar image
0

Answer by TackCo · Jan 24, 2017 at 08:07 AM

@Rennat, Since I can't add a reply for some reason, I will post it as a separate answer. I enjoy that there is a script that uses angles instead of vector movement. This way, the gun will be less accurate at distance. A simple vector movement means that while the gun will be inaccurate, it will be just as inaccurate at infinity distance than at point blank distance. Thanks!

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 FPSworrior · Dec 07, 2018 at 11:46 PM

Heres the script that I use for my weapons. It may not be efficient but it does get great results.

 public float accuracy;
 public float aimAccuracy;
 public float hipAccuracy;
 private Quaternion shotAcc;
 
 public Transform shotPoint;
 
 if(Input.GetKey(KeyCode.Mouse1))
         {
             accuracy = aimAccuracy;
             camObj.fieldOfView = 40;
            
         }
         else
         {
             camObj.fieldOfView = 50;
             accuracy = hipAccuracy;
         }
 
 if (semiauto == true && !isReloading)
         {
              if (Input.GetKeyDown(KeyCode.Mouse0) && !Input.GetKey(KeyCode.LeftShift) && Time.time > nextFire)
                 {
                     if (magAmmo > 0)
                     {
 
                         shotAcc = Quaternion.Euler(Random.Range(-accuracy, accuracy), Random.Range(-accuracy, accuracy), 0);
                         shotPoint.localRotation = shotAcc;
 
 
                         Shot();
                         shotSound.Play();
                         magAmmo--;
                         shotsFired++;
                         muzzleFlash.Play();
                     }
                 }
             
         }
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

how to make enemies have health and get damaged by bullets 3 Answers

Bullets kill enemy... but continue to travel? 1 Answer

why wont machine gun follow first person controller 2 Answers

Hi all. I am new to unity 3d. I have a problem i have very basic scene and still my FPS is very low( less than 30 ). Please help me. 2 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