• 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
1
Question by Jackyinf · Jun 27, 2015 at 11:02 PM · rotation3dquaternionsphere

Rotate around sphere using Quaternions with full radius

Hi,

I am trying to shoot projectiles from the player's position, who is on a surface of the sphere, and make the projectiles rotate around the sphere. Here's (sloppy) 3D representation:

alt text

Same stuff but from the side view:

alt text

The projectiles rotate correctly when initializing projectiles from emit position 1 a.k.a initial position.

When I translate player's position and initialize a new projectile, it has smaller radius. The required direction should be the direction in w$$anonymous$$ch the green arrow is pointing at.

The code for rotation I use:

     public float projectileSpeed = 20f;     // Speed of the projectile
     public GameObject projectile;           // Projectile's template
     private GameObject aProjectile;         // Instantiated projectile
     public Vector3 centerPosition;          // origin (0,0,0)
 
     void Update() {
         if (Input.GetButtonDown("Fire1"))
         {
             Shoot(transform.position);
         }
 
         if (aProjectile != null)
         {
              var axis = Vector3.up;   // does not matter, in w$$anonymous$$ch direction... Let's go up, for instance...
              aProjectile.transform.RotateAround(centerPosition, axis, projectileSpeed * Time.deltaTime);
         }
     }
 
     public void Shoot(Vector3 fromPosition)
     {
         aProjectile = Instantiate(projectile, fromPosition, Quaternion.identity) as GameObject;
         t$$anonymous$$sTransform = aProjectile.transform;
     }

Camera is always facing the sphere. I've omitted player's movement script around the sphere for brevity.

unity-q2.png (7.0 kB)
unity-q1.png (7.3 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jackyinf · Jul 20, 2015 at 03:30 PM

I got it working by making such projectiles w$$anonymous$$ch consist of 2 parts: empty gameobject, w$$anonymous$$ch is used as origin, and inside there is nested sphere, w$$anonymous$$ch is offset.

Then, I pass a position and direction in w$$anonymous$$ch I'd like to shoot. LookAt makes sphere GameObject to look at its origin point. As a result, a projectile is shot in the straight direction.

         public float projectileSpeed = 20f;         // Speed of the projectile
         public GameObject projectileTemplate;       // Projectile's template
     
         private GameObject projectileClone;         // Instantiated projectile
         
         private void Update()
         {
             if (projectileClone != null)
             {
                 var axis = Vector3.right;                       // axis around w$$anonymous$$ch are projectiles going to rotate. Projectile's direction is 90 degrees CCW
                 var qR = Quaternion.AngleAxis(Time.deltaTime * projectileSpeed, axis);      // calculate the rotation around axis
                 projectileClone.transform.rotation *= qR;       // apply the rotation
             }
         }
     
         /// <summary>
         /// Instantiates projectiles
         /// </summary>
         /// <param name="fromPosition">Position, where projectiles start flying from</param>
         /// <param name="forward">Direction to shoot projectiles towards</param>
         public void Shoot(Vector3 fromPosition, Vector3 forward)
         {
             if (projectileClone != null)
                 Destroy(projectileClone);
     
             // We use LookAt to instantiate projetiles on the right place. Projectiles should be comprised from 2 parts: center point and GameObject
             // w$$anonymous$$ch rotates around its center. GameObject is on the surface of the planet.
             // We also need -1 for projectiles to appear not on the other side of the planet. 
     
             projectileTemplate.transform.LookAt(fromPosition * -1, forward);         
             projectileClone = Instantiate(projectileTemplate);
         }
 
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

2 People are following this question.

avatar image avatar image

Related Questions

Character rotating in local space instead of world space? 1 Answer

"Free" rotation about a sphere 0 Answers

Rotation Question in Top-down 3D Game 0 Answers

Lerp rotation relative to sphere 0 Answers

Quarternion.LookRotation is only rotating my game object on one axis 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