Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by falconstrike209 · Dec 11, 2021 at 04:35 PM · rotationinstantiatefor-loopspreadshotgun

Shoot in proper direction with spread?

I'm trying to make a shotgun in a 2D game. I currently have this:

      public void Shoot()
      {
          int rotZ = -10;
          for (int i = 0; i < 3; i++)
          {
              Instantiate(bulletPrefab, firePoint.position, Quaternion.Euler(0, 0, rotZ));
              rotZ += 10;
          }
      }

this works great for the spread, but completely ignores the rotation of my firePoint. I am aware this is because I replaced firePoint.rotation with rotZ, but what I want is to be able to use rotZ for the spread as well as firePoint.rotation for the direction at the same time. Does anyone know how I might do this? thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AaronBacon · Dec 11, 2021 at 04:48 PM

A very easy way would be to just have the Bullet script randomly rotate itself slightly on Awake:

⠀

 public class Bullet : MonoBehaviour
 {
     //Awake will be called when the bullet is created
     void Awake()
     {
       transform.Rotate(0f,0f,Random.Range(-10f,10f));
     }
 }
 

Though you could also do this when spawning the bullet like so:

⠀

     private void Shoot()
     {
         for (int i = 0; i < 3; i++)
         {
             GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
             bullet.transform.Rotate(0f,0f,Random.Range(-10f,10f));
         }
     }

Comment
Add comment · Show 5 · 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 falconstrike209 · Dec 11, 2021 at 05:05 PM 1
Share

I just put it on like this:

     public void Shoot()
     {
         int rotZ = -10;
         for (int i = 0; i < 3; i++)
         {
             GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
             bullet.transform.Rotate(0f, 0f, rotZ);
             rotZ += 10;
         }
     }

and it worked like a charm. Thanks!

avatar image AaronBacon falconstrike209 · Dec 11, 2021 at 05:12 PM 0
Share

Ah right, I think I misunderstood what you were trying to do, but yep that works for having a set spread, which thinking about it is probably better for 2D

avatar image falconstrike209 · Dec 11, 2021 at 05:31 PM 0
Share

anyway, while you're here, do you know how I might go about adding knockback to the player when shooting? The gun is a child object of the player, if that means anything about how it would work

avatar image AaronBacon falconstrike209 · Dec 11, 2021 at 05:55 PM 0
Share

Depends on what kind of knockback you want. Assu$$anonymous$$g the player has a RigidBody Component you can use this to knock them back in the reverse direction of their gun:

⠀

 ⠀   Rigidbody2D rb2d;
       public float blastForce = 10f;
      void Awake()
      {
        rb2d = GetComponent<RigidBody2D>();
      }

 public void Shoot()
  {
      int rotZ = -10;
      for (int i = 0; i < 3; i++)
      {
          GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
          bullet.transform.Rotate(0f, 0f, rotZ);
          rotZ += 10;
      }
         Vector3 knockbackDirection= transform.position - firepoint.position;
         direction.Normalize();
         rb2d.AddForce(direction * blastForce);
  }

⠀ I wrote this code a while back so it might not be exactly what you need, if you need more simple knockback (IE, just push the player backwards horizontally a bit) you can look into Rigidbody2D's AddRelativeForce function

avatar image falconstrike209 AaronBacon · Dec 11, 2021 at 06:22 PM 1
Share

I changed it to modify velocity instead of addForce, as well as added a bool to cancel the player movement as that had been interfering, but it's working exactly how I wanted it to! thanks so much dude, you're awesome!

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

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

Making a shotgun shooting 45 degrees to the left of the mouse 1 Answer

Instantiate rotation is not correct, why? 1 Answer

Is there a way to set the current rotation of an object to 0,0,0? 2 Answers

Instantiating prefab of object like it was in the scene 0 Answers

how do I keep each cloned object at a certain point after trigger 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