• 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 aljovidu · Apr 30, 2014 at 03:16 PM · 2drotationquaternion2d rotation

[2D] Rotations become tighter the further left they are?

What I'm trying to do is instantiate multiple projectiles based on one rotation, and slightly adjust the rotations of the projectiles through iteration to create a 'cone attack' effect. However, it only works well when the target is to the right of the origin/attacking unit. The rotations of the projectiles become tighter and tighter the further left the target is, until they're all overlapping when the target is directly left of the origin. Any help or insight would be greatly appreciated.

                 for (var i = 0; i <= NumberOfPulses; i++)
                 {
                     var didCenter = false;
                     for (var n = 1; n <= BulletsPerPulse; n++)
                     {
                         if (n == 0) continue;
                         _playerPos = _playerTransform.position;
                         _myPos = _myTransform.position;
 
                         // rotation toward player -->
                         var diff = _playerPos - _myPos;
                         diff.Normalize();
                         var rotZ = Mathf.Atan2(diff.y, diff.x)*Mathf.Rad2Deg;
                         var newRotation = Quaternion.Euler(0f, 0f, rotZ);
                         var newRot1 = newRotation;
                         var newRot2 = newRotation;
                         // <--
 
                         newRot1.z += (BulletSpreadVariance*n);
                         newRot2.z -= (BulletSpreadVariance*n);
 
                         bullet1 = Instantiate(Projectile, _myPos, newRot1) as GameObject;
                         bullet2 = Instantiate(Projectile, _myPos, newRot2) as GameObject;
                         if (!didCenter)
                         {
                             bullet3 = Instantiate(Projectile, _myPos, newRotation) as GameObject;
                             if (bullet3)
                             {
                                 bullet3.rigidbody2D.AddForce(bullet3.transform.right*BulletSpeed);
                                 bullet3.name = n + " " + newRotation + " " + " bul1 " + (BulletSpreadVariance*n);
                                 Destroy(bullet3, BulletLifetime);
                                 didCenter = true;
                             }
                         }
 
                         if (!bullet1 || !bullet2) continue;
                         bullet1.rigidbody2D.AddForce(bullet1.transform.right*BulletSpeed);
                         bullet2.rigidbody2D.AddForce(bullet2.transform.right*BulletSpeed);    
 
                         bullet1.name = n + " " + newRot1.z + " " + " bul1 " + (BulletSpreadVariance*n);
                         bullet2.name = n + " " + newRot2.z + " " + " bul2 " + (BulletSpreadVariance*n);
 
                         Destroy(bullet1, BulletLifetime);
                         Destroy(bullet2, BulletLifetime);
 
                     }
                 }
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
0

Answer by aljovidu · May 01, 2014 at 06:34 AM

StarManta on the forums was able to enlighten me that I am misusing quaternions, and he helped point me in the right direction. You can view the thread here: http://forum.unity3d.com/threads/243575-2D-Rotations-become-tighter-the-further-left-they-are?p=1611741&posted=1#post1611741

In short, the answer was to not adjust the .z value directly, but instead modify the rotation using the AngleAxis method of the quaternion helper class.

 var newRot1 = newRotation * Quaternion.AngleAxis(newRotation.z + n * BulletSpreadVariance, Vector3.forward)
 var newRot2 = newRotation * Quaternion.AngleAxis(newRotation.z - n * BulletSpreadVariance, Vector3.forward)

instead of

 var newRot1 = newRotation;
 var newRot2 = newRotation;
 newRot1.z += BulletSpreadVariance * n;
 newRot2.z -= BulletSpreadVariance * n;
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

What causes this object to re-rotate itself after it hits it's destination? 1 Answer

How do I make a 2D sprite smoothly rotate upright? 1 Answer

How do I rotate a 2D object based on it's velocity? 1 Answer

Trying to rotate a 2D image over time to a specific angle, but it's rotating its Y value every time 1 Answer

Help with rotating a sprite with mouse. 2 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