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 TargonStudios · Dec 04, 2013 at 05:12 PM · bulletfirespread

Spreading Fire (Bullets)

So i currently have created a tank, but its Shell shots are bang on perfect everytime. What i want is to have them be a little off somtimes, not always perfect. How could i do somthing like this through script? (I dont want a script, just an idea how to do this). Also, the bullet spread has to be adjustible, because the bullet spread will increase/decrease due to movement, if on the ground, etc. Thanks in advance. (I work in JS)

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
2

Answer by EX_Darius · Dec 05, 2013 at 12:38 AM

You can create a random spread by using Random.range , use the value for the rotation of the instantiated object, after that apply the force/movement to the object.

a short example in C#:

 public float Stray;
 private float NextShot;

 void FireWeapon()
 
 {
 
 if(Time.time > NextShot)
 
     {
         var randomNumberX = Random.Range(-Stray, Stray);
         var randomNumberY = Random.Range(-Stray, Stray);
         var randomNumberZ = Random.Range(-Stray, Stray);
         Rigidbody YourProjectileClone =(Rigidbody)Instantiate(YourProjectile,position, rotation); // instantiate projectile
         YourProjectileClone.transform.Rotate(randomNumberX, randomNumberY, randomNumberZ);
         YourProjectile.AddForce(YourProjectile.transform.forward * 6000);
         NextShot = Time.time + 0.1f;
 }
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 ethanwgross · Jun 15, 2014 at 05:52 PM 0
Share

Just a heads up, you can save yourself the calculation of the Z axis. In this instance, it is not necessary. Just replace with:

 YourProjectileClone.transform.Rotate(randomNumberX, randomNumberY, 0.0f);

avatar image
0

Answer by TimBorquez · Dec 04, 2013 at 08:54 PM

before every shot i would use random range to determine some inaccuracy to add on to your shot angle,

and for the max number in random i would put a variable there and make it a higher/lower number depending on how accurate you want it

Comment
Add comment · Show 3 · 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 TargonStudios · Dec 04, 2013 at 10:23 PM 0
Share

And then just set the rotate of the spawn depending on the number that comes up?

avatar image TimBorquez · Dec 04, 2013 at 10:39 PM 0
Share

yeah when you instantiate yo bullet, i guess you could then give the bullet velocity that includes some of that random range, i can give more detail later when i get home

avatar image TimBorquez · Dec 04, 2013 at 10:42 PM 0
Share

actually scratch that, i think you can set the rotation when you instantiate it (the quaternion) and then just make the bullet go straight, that should work i think...

avatar image
0

Answer by alienbaby · Dec 04, 2013 at 11:15 PM

I'd suggest adding a small offset vector to the aim vector you have, of a random direction with the magnitude scaled appropriately depending on how your modelling the physics of the bullet. This would separate accuracy from the physical properties of the gun, which is ideal for a game unless you are trying to simulate real world interactions.

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make my bullets spread when fired 3 Answers

[Unity2D] Make fire spread through tiles in Platformer 0 Answers

Setting bullet instansiate direction? help? 1 Answer

Multiple Bullets 2 Answers

2D Bullet Hell Game - Bullet Patterns 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