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 DrinkableGames · Dec 06, 2021 at 09:10 PM · rotationinstantiateshootingfor-loopinstantiate prefab

Instantiate rotation is not correct, why?

I have a Player that shoots bullets from a gun. Depending on the gun, there are different amounts of bullets. With my Shoot() method I want the instantiated bullets to have a fixed spread each time they shoot.

I tried that with a for loop:

 void shoot()
     {
         //Set Spread
         float spread = 10;
         float currentSpread = 0;
 
         //Instantiate Bullet
         for (int i = 0; i < amount; i++)
         {
             //Get Spread
             if (i == 0)
             {
                 currentSpread = 0;
             }
             else if (i == 1)
             {
                 currentSpread = spread;
             }
             else if (i == 2)
             {
                 currentSpread = -spread;
             }
             else if (i == 3)
             {
                 currentSpread = spread * 2;
             }
             else if (i == 4)
             {
                 currentSpread = -spread * 2;
             };
 
             //Instantiate
             Bullet = Instantiate(PreFab, Canvas.position, Quaternion.Euler(0, 0, Player.rotation.y + currentSpread));
 
             //Set Parent
             Bullet.transform.SetParent(Canvas, false);
 
             //Set Name
             Bullet.gameObject.name = "TeamBulletOne";
         }
     }

The "Canvas" is where the Bullets (2d) spawn, the rest of the world is 3d.


However, when I shoot it randomly either instantiates the bullets with a slightly off rotation or they spawn with about 2 degrees difference, not with the 10 degrees that are set in the Shoot() position.


I have no idea why and no matter how often I change or go through the method, I can't find out why it isn't working...

I don´t have anything else that affects the shooting mechanism, the colliders on my bullets are set to ignore the player and the other bullets and gravity is of as well.


Does anyone have an idea what to do?

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

Answer by metalted · Dec 06, 2021 at 09:35 PM

You might want to try Quaternion.AngleAxis(player.up, currentSpread);. This will create a rotation around the player y-axis, with a certain degree. You might get weird results because player.rotation.y is the y component of a Quaternion, and Quaternions are 4 dimensional hocus pocus. Its not the same as player.transform.localEulerAngles, which is the rotation you see in the inspector, which you can't really use because of the rollback to 0 after 360 (or -180, 180 not sure atm).


Also, I wanted to say, instead of using -else if-, you could use -switch- statement. But what would be even better is using the following ternary operator: currentSpread = i % 2 == 0 ? (i / 2) * -spread : (i + 1) / 2 * spread;

It is the same as your if else, but you dont need to rewrite for every i value. (If you are not familiar with the ternary operator: value = condition ? if condition is true : if condition is false. So in this case: currentSpread = i is even ? calculate value for even i : calculate value for odd i. )

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 DrinkableGames · Dec 07, 2021 at 11:14 AM 0
Share

The ternary operator is a great improvement I didn't think of, thank you! The Quaternion.AngleAxis(currentSpread, player.up) though works for the z-axis but generates random rotations for the other axis as well. I tried setting x and y rotation zu 0, but then I just get the same results as before...

I move my bullet with Rigidbody.AddForce(transform.forward), could it be that this causes the problem?

EDIT: Tried with CharacterController and transform.Translate, both didn't solve my problem, instead made the bullet behavior even weirder

avatar image metalted DrinkableGames · Dec 07, 2021 at 11:47 AM 0
Share

Oh my bad, Quaternion.AngleAxis is not really the right way to go. I wasn't able to test it. Maybe this is what you are looking for: transform.rotation * Quaternion.Euler(0, currentSpread, 0). This will basically take the players rotation, and rotate around the players y axis 'currentSpread' degrees.

I see you wanted to rotate around the z-axis. Doesnt really make a difference. I guess the code you want to use is as follows: Bullet = Instantiate(PreFab, Canvas.position, Player.rotation * Quaternion.Euler(0, 0, currentSpread));

avatar image DrinkableGames · Dec 07, 2021 at 01:05 PM 0
Share

I tried that and additionally had to adjust some rotations //Instantiate Bullet = Instantiate(PreFab, Canvas.position, Player.rotation * Quaternion.Euler(0, 0, currentSpread)); Fix Rotation Bullet.transform.rotation = Quaternion.Euler(0, 0, currentSpread); Bullet.transform.localRotation = Quaternion.Euler(Bullet.transform.localRotation.x, Bullet.transform.localRotation.y, currentSpread);


Now it works better than before, so thanks! It still gives a little bit off z rotations (9,894 instead of 10 for example, no idea why), but that's fine for now!

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

198 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

Related Questions

Shooting bullets in different patterns 0 Answers

Shooting with variable vector3 0 Answers

Shoot in proper direction with spread? 1 Answer

Network.Instatiate Problem 0 Answers

How can I spawn balls in every direction around a source (making a sphere) and then make the balls move away from that source? 1 Answer


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