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 ItsLurq · Dec 19, 2014 at 03:22 AM · c#2dbullet

Creating a spread of bullets

I have gotten my code so that it fires two bullets, but currently they fire to the exact same position, but I have been trying many different ways (without success) to angle the second bullet slightly to one side at a fixed angle.

My code:

 if (lookRight) {
             //first bullet
             Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
             Vector3 dir = (Input.mousePosition - sp).normalized;
             Rigidbody2D bPrefab = Instantiate (staffBulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
             bPrefab.rigidbody2D.AddForce (dir * 650);
             coolDown = Time.time + attackRate;
             float rot_z = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
             bPrefab.rigidbody2D.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 45);

             //second bullet
             Vector3 dir2 = (Input.mousePosition - sp).normalized;
             Rigidbody2D bPrefab1 = Instantiate (staffBulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
             bPrefab1.rigidbody2D.AddForce (dir2 * 650);
             coolDown = Time.time + attackRate;
             float rot_z1 = Mathf.Atan2(dir2.y, dir2.x) * Mathf.Rad2Deg;
             bPrefab1.rigidbody2D.transform.rotation = Quaternion.Euler(0f, 0f, rot_z1 - 45);
         } 

As always, any help what so ever is greatly appreciated! :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LRG · Dec 19, 2014 at 01:20 PM

To spread the bullets randomly you can probably do something like:

 Vector3 newDir = (dir.x + Random.Range(0.0f, 0.1f), dir.y + Random.Range(0.0f, 0.1f), dir.z + Random.Range(0.0f, 0.1f)).normalized;
 Rigidbody2D bPrefab = Instantiate (staffBulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
 bPrefab.rigidbody2D.AddForce (dir * 650);

This will simply vary the values a little and normalize it again.

Edit

If you actually want to spread the bullets by a fixed angle you can try something like:

 Quaternion rotation = Quaternion.Euler(0f, 0f, rot_z1 - 45); 
 Vector3 newDir = rotation * dir;
 bPrefab.rigidbody2D.AddForce (newDir * 650);

That should rotate the direction vector by your rotation as well.

Though, you might have a simpler alternative. You are (presumably) already rotating your sprite. You can simply add the force locally. Though unfortunately the 2D tools don't seem to have an AddRelativeForce method, you can:

bPrefab.rigidbody2D.AddForce(bPrefab.transform.forward * 650);

This should essentially just propel the bullet forward with respect to itself, which is probably what you want.

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 ItsLurq · Dec 19, 2014 at 01:47 PM 0
Share

Thanks for the reply! But what I am wanting (and didn't state in the question sorry (will edit now)) is that its a fixed angle, so that, for example, 3 bullets are fired on each click, at fixed angles.

avatar image LRG · Dec 19, 2014 at 02:18 PM 0
Share

Ok! Then probably just doing a pseudo addRelativeForce should do, considering that you are rotating the object already. I've updated the answer.

avatar image ItsLurq · Dec 20, 2014 at 05:59 AM 0
Share

This is working, but to an extent. There is now a problem based on the mouse position, the closer the mouse to the player, the wider the angle is, and the further away the mouse is, the smaller the angle!

avatar image
0

Answer by HarshadK · Dec 19, 2014 at 08:08 AM

You are applying the force on both the bullets in the same direction. Try changing the direction of applying force to second bullet.

 bPrefab1.rigidbody2D.AddForce ((dir2 + new Vector3(0.1f, 0f, 0f) )* 650);

In above code the direction of applying is deviated by 0.1f in x axis. Just get the logic and you can apply deviation in any way you want.

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 ItsLurq · Dec 19, 2014 at 08:15 AM 0
Share

The issue with this is that it is causing the bullet to travel much faster then the other projectile, but I believe this is a good start :)

avatar image HarshadK · Dec 19, 2014 at 02:11 PM 0
Share

You can actually set the less value for force like you can change 650 to maybe 600 so the force applied balances out.

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D How to fire bullets on the y axis. 0 Answers

2D bullet spray problems 1 Answer

Multiple Cars not working 1 Answer

Transform.translate bullets problem 0 Answers

2d bullet shot at bullet emitter 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