• 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
Question by sk00ter · Apr 04, 2017 at 12:39 AM · rotationrotation axisrelative rotation

Rotating On Two Axes Independantly

Ok, so this could be hard for me to explain, but I will try my best and make use of extensive images. This is a 3-Dimensional math problem, and I am having trouble finding the answer.

So basically, I have written a script that creates a "ring" of orbiting GameObjects (in my example, several Spheres) that are instantiated as a Child under a central GameObject (in this example, a Cube).

The cube's origin is 0,0,0 with no rotation. The spheres are generated equidistant from the origin Cube when given a distance (public int dist) at an angle calculated based on the number of objects being generated (public int num). This script is placed on the Cube object:

 Vector3 origin = transform.position;
         for (int i = 0; i < num; i++){
             int angle = 360 / num;
             int a = i * angle;
             var pos = NewCircle(origin, dist, a);
             GameObject newPrefab = Instantiate(prefab, pos, Quaternion.identity);
             newPrefab.transform.parent = transform;
         }

and

 Vector3 NewCircle(Vector3 center, float radius, int a)
     {
         Debug.Log(a);
         float ang = a;
         Vector3 pos;
         pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
         pos.y = center.y + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
         pos.z = center.z;
         return pos;
     }

So that's the basic creation code. It is built on the X and Y coordinates of the scene and it ends up looking like this: Screen 1

The problem comes when I start rotating these objects.

The goal is for this object to rotate on the X and Z axes, with the Y rotation remaining unchanged (at 0) so when Trail Renderers are added to the Sphere objects, it creates a very particular pattern.

The problem is, I can't rotate on the X and Z axes without affecting the angle on the Y axis. I came up with a solution that technically achieves this. However, the Trail Renderers do not "trail" on the Y axis. Why? Because, instead, I have positioned the camera and rotated IT on the Y axis, which gives the illusion of the object rotating. However, while it shows the correct positioning and rotation of the object, the effect is not what I want. Rotation code:

 void Update () {
         targetRotation *= Quaternion.Euler(0, 0, 2);
         transform.rotation= Quaternion.Slerp (transform.rotation, targetRotation , 10 * Time.deltaTime);
     }

Basically, the "ring" rotates counter clockwise, on the Z axis (See previous screenshot), while the Camera, positioned under the ring, looking up (Orthographic; Rotation: Y=90;), rotates on its local Z axis, or rather, the Scene's X axis.

In-Game, the Object appears to be rotating along the Scene's X axis, but really is just the camera rotating: Screen 2

So, as you can see above, the Trail Renderers do not render along the X rotational plane, because the object, is not rotating along it. However, If I add X rotation to the Objects rotational code:

 void Update () {
         targetRotation *= Quaternion.Euler(2, 0, 2);
         transform.rotation= Quaternion.Slerp (transform.rotation, targetRotation , 10 * Time.deltaTime);
     }

the rotational planes combine, and begin affecting the Y axis' angle of rotation. It needs to stay at 0.

I am sure someone smarter than I has a mathematical way of interpolating the Y axis to remain at 0. Any help is much appreciated.

Thank you in advance.

screen-3.png (11.1 kB)
screen-2.png (70.8 kB)
Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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

79 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

Related Questions

Rotate an object's yaw, pitch, and roll relative to the Game Camera Axis. 1 Answer

Object Rotating On Z Axis No Matter What 0 Answers

Rotating an object in a specific direction to a specific point 0 Answers

Rotate cube non-relative to current rotation. 1 Answer

Problem when i rotate a 2d object aroun Z-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