Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 xToxicInferno · Feb 17, 2010 at 07:09 PM · rigidbodyaddforcebulletspread

How to make my bullets spread when fired

Alright i am making a action/arcade game, where you are a Tank, and you have to go around shooting enemies to gain gems. As it is a arcade type game, i made a power-up that upgrades your gun, from being a single shot, to shooting 3 bullets at the same time. I got that whole part worked out, the problem is, the bullets are instantiated on 3 different SpawnSpots (game object), and they all fly forward of the player. The thing that i want to do is have it so one goes stright forward, the one on the right goes right at a angle, and the one on the left goes at angle to the left. I have no idea how to do this, i figure it is part of rigidbody.AddForce but i do not know which would work, so i could turn, and it would always shoot forward of me, yet slightly angled so it spreads.

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
6
Best Answer

Answer by Sebas · Feb 17, 2010 at 08:22 PM

You would have to apply a rotation to the bullets after they are instantiated but before you add a force to them. I have a script laying around which gives a bit of a random spread. You can easily modify this so that you have a fixed angle to the left and a fixed angle to the right for those bullets.

var bulletPrefab:Transform; var strayFactor : int;

function Update() { if(Input.GetButtonDown("Jump")) {

     var randomNumberX = Random.Range(-strayFactor, strayFactor);
     var randomNumberY = Random.Range(-strayFactor, strayFactor);
     var randomNumberZ = Random.Range(-strayFactor, strayFactor);

     var bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
     bullet.transform.Rotate(randomNumberX, randomNumberY, randomNumberZ);
     bullet.rigidbody.AddForce(bullet.transform.forward * 10000);
 }

}

So the bullet is instantiated in the exact rotation which you are facing (given that you attach the script to your main camera) and then it adds a rotation (in this case random) and shoots the bullet off by adding the force.

Comment
Add comment · Show 2 · 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 xToxicInferno · Feb 18, 2010 at 12:26 AM 0
Share

Yeah this worked perfectly. Well it with a little modification it worked, but at least it pointed me in the right direction! Thanks so much for your help with this, and my other questions Sebas.

avatar image Matthew Scott · Dec 23, 2013 at 06:09 PM 0
Share

I wouldn't bother giving a bullet a rotation around the Z (forward) axis, it wont make any difference to its direction. You might as well save a calculation and just make it 0.0f

avatar image
0

Answer by Me · Dec 17, 2010 at 04:29 AM

Any Chance you could post you modification?

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 acsended · Nov 16, 2011 at 09:40 AM 0
Share

ahh it's good to see someone is working on the same thing as me but i already fixed this subject but what im now trying to do is reducing the spread when i press/hold right mouse button this is ai$$anonymous$$g down sight

i know it's a question in an question but i cant find it anywhere and this is the fifth time i came to this question so if someone can help me

if (Input.Get$$anonymous$$ouseButtonDown(1)) {

 direction.x += Random.Range(-$$anonymous$$inspread,$$anonymous$$inspread);

 direction.y += Random.Range(-$$anonymous$$inspread,$$anonymous$$inspread);

 direction.z += Random.Range(-$$anonymous$$inspread,$$anonymous$$inspread);
 }

else { ....................... }

fixed it check above could be usefull for more realistic gun fire

avatar image
0

Answer by acsended · Nov 16, 2011 at 12:22 PM

var Maxspread: float = 0.03;

var Mimspread: float = 0.01;

var Spread: float = 0.01;

//.....

// the total spread along the x y and z

//....

direction.x += Random.Range(-Maxspread,Maxspread);

direction.y += Random.Range(-Maxspread,Maxspread);

direction.z += Random.Range(-Maxspread,Maxspread);

 if (Input.GetButton("Fire2")) 

{

 Maxspread = Minspread;

}

else {

Maxspread = Spread;

}

@@@@@@@@@@@@@@@@@@@@@@@@@

if u put this in the fireoneshot script or what function u have then the bullets should spread and if u press right mouse button it should spread less handy for if u can aim down sight a little with ur tank

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

make bullet move forward. 2 Answers

rigidbody addforce behaves different every time 2 Answers

Bullet Launcher Script 1 Answer

Very simple water bouyancy script not working? 1 Answer

Shooting with the maneuvering of the ship 0 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