Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 theElderOne · Jan 15, 2018 at 09:49 PM · 2d-platformer2d-physicsprojectile

Help Adding Local Force and Rotating in a For Loop

I'm making an action platformer and the game play centers around the player shooting a shotgun like this alt text

So I already have the gun shooting singular bullets and dealing damage to enemies what I need help with is two things. The first one is simple here's my code, I need help adding velocity along a local axis. the next thing is a little bit more difficult, I need to rotate each bullet between the negative and positive value of bulletSpreadAngle. so I can then add the local force along the x axis.

 float numOfBullets = 5f;
     float bulletSpeed = 10f;
     float bulletSpreadAngle = 45f; // How far the farthest bullet will be tilted
     GameObject bulletPrefab; // Defined in the editor, this is the prefab that all bullets are spawned from
     
     void ShootWeapon()
         {
             // bullet's rotation = spread distance / number of bullets
             // Every iteration in the loop add onto the rotation and rotate the bullet that much
             for (int i = 0; i < numOfBullets; i++)
             {
                 for (float j = -bulletSpreadAngle; j < bulletSpreadAngle; j += bulletSpreadAngle / numOfBullets)
                 {
                     GameObject bullet = Instantiate(bulletPrefab, gunEnd.position, Quaternion.identity);
                     Rigidbody2D projectileRigidbody = bullet.GetComponent<Rigidbody2D>();
     
                     if (transform.localScale.x > 0)
                     {
                         //bullet.transform.rotation = new Quaternion(Quaternion.identity.x, Quaternion.identity.y, Quaternion.identity.z * j, Quaternion.identity.w);
                         projectileRigidbody.velocity = new Vector3( bulletSpeed, projectileRigidbody.velocity.y);
                     }
                     else if (transform.localScale.x < 0)
                     {
                         projectileRigidbody.velocity = new Vector2(-bulletSpeed, projectileRigidbody.velocity.y);
                     }
                 }
             }
         }
 
 
 




quick-pic.png (33.6 kB)
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 Diukrone · Jan 16, 2018 at 02:16 AM

You can try using Vector3(new Vector3(0,0,0) and Mathf.SmoothDamp and Mathf.Lerp. Check it on Unity API!

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 theElderOne · Jan 16, 2018 at 02:18 PM 0
Share

That's not exactly what I'm looking for. Those change something in for instance the Update loop. What I'm looking for is some way to add on say 9 degrees every iteration of a for loop.

avatar image
0

Answer by Dray · Jan 16, 2018 at 03:33 PM

Sine and cosine will do the job:

 projectileRigidbody.velocity = new Vector2(
     Mathf.Sin(j),
     Mathf.Cos(j)
 ) * bulletSpeed;
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

80 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 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 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

I need to shoot a projectile in my 2D platformer in the opposite direction my player is dashing which can be in any direction the player points the joystick 2 Answers

Simple 2D Character Movement does not work properly 1 Answer

Help Me! I want to make a pixel fairy! 1 Answer

How can I make an object stop all momentum and hold it's position in air? 1 Answer

Character gets stuck on projectile, despite different layers,Player gets stuck on projectile, despite different layers 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