• 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 CubicApocalypse · May 07, 2013 at 09:57 PM · rotationorbitplanetmoon

Axial tilt and moons

I found a script that makes an object spin and orbit the origin.

 #pragma strict
     
 function Start () {
 
 }
 
     var PlanetRotateSpeed : float = -25.0;
     var OrbitSpeed : float = 10.0;
      
     function Update() {
     // planet to spin on it's own axis
     transform.Rotate(transform.up * PlanetRotateSpeed * Time.deltaTime);
      
     // planet to travel along a path that rotates around the sun
     transform.RotateAround (Vector3.zero, Vector3.up, OrbitSpeed * Time.deltaTime);
     
     }

What I'd like to do is have moons orbit moving planets and tilt the planets' Y axes. When I try tilting a planet that uses this script, it rotates chaotically. Does anyone have a script that allows axial tilt and orbiting moving objects?

Comment
Add comment · Show 2
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 hoy_smallfry · May 07, 2013 at 10:23 PM 0
Share

Is it rotating about itself chaotically or around the orbit chaotically. If its the former, I think I know what's up.

avatar image CubicApocalypse · May 07, 2013 at 10:26 PM 0
Share

It's rotating chaotically; it orbits as expected.

1 Reply

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

Answer by hoy_smallfry · May 07, 2013 at 10:46 PM

The documentation on RotateAround() states that it affects both the position and rotation of the object that it is used on in world coordinates. Rotate(), on the other hand, rotates the object around its center on local coordinates by default. Therefore, this script is rotating the object in two different directions and by different coordinate systems.

The real problem, though, is that Transform.up is in world coordinates, yet its being used to transform in local coordinates. You don't notice anything when the moon is upright in the world, because Vector.up and transform.up are both (0, 1, 0) in this state. Therefore, the orbit's rotation axis doesn't transform the direction of transform.up to anything other than (0, 1, 0). The transform.up is then used as a local coordinate, which also conveniently happens to be the local vector for up (0, 1, 0). So the result appears that the moon's rotation axis is fine.

However, the moment you tilt it, the bad logic becomes apparent because the orbit 's rotation axis modifies the direction of transform.up to something that is not (0, 1, 0). Since it no longer happens to be the local vector for up, its now rotating the moon on a new axis every frame; hence, your chaotic movement.

There are two ways to solve this. First one is to bring the moon rotation to world coordinates as well, so that it uses transform.up in the proper coordinates:

 // planet to spin on it's own axis
 transform.Rotate(transform.up * PlanetRotateSpeed * Time.deltaTime, Space.World);

or, you can keep the moon rotation in local corrdinates, and just change the axis to the local vector for up:

 // Vector.up in local *is always* transform.up in world
 transform.Rotate(Vector.up * PlanetRotateSpeed * Time.deltaTime);

Both of them give the same output, but one fix does it in reference to local coordinates, the other in world coordinates.

Hope that helps!

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 CubicApocalypse · May 08, 2013 at 02:30 AM 0
Share

That fixed my rotation problem, but how can I set the moon to orbit a planet that is orbiting the sun?

avatar image hoy_smallfry · May 08, 2013 at 07:14 PM 0
Share

If you look at the documentation for RotateAround(), the first parameter you give it is the point it rotates around and the second is the axis of rotation. This function is world coordinates only, so you want to make sure that the values you pass to it are in world coordinates as well.

You just need to specify that you want to use the position of another game object ins$$anonymous$$d of Vector3.zero, and then specify any special axis as the second argument.

Are you familiar with using references to other game objects in a script?

avatar image CubicApocalypse · May 08, 2013 at 09:51 PM 0
Share

Not at all.

avatar image hoy_smallfry · May 09, 2013 at 06:00 PM 1
Share

You need to make a public variables in your script, like this:

 var centerObject : GameObject = null;
 var orbitAxis : Vector3 = Vector3.up;

Center object is a GameObject type so any game objects be set to it, and the orbit axis will define how you want the object to rotate. When you create a public variables like this, they will appear in the inspector view for every game object that has this script. Since the centerObject variable is set to null in the script, the value in the inspector will be "None".

You can then set whatever game object you want to that variable by dragging it from the hierarchy view to the variable in the inspector view, and it will reference the object (aka will link to it).

From there, you just change this line of code:

 transform.RotateAround (centerObject.transform.position, orbitAxis, ...

And now, you have something that will orbit around any object in around any axis that you set them to from the editor.

avatar image CubicApocalypse · May 09, 2013 at 06:58 PM 0
Share

Got that figured out in a different thread. It works perfectly 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.

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

13 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

Related Questions

How to make a moon orbit your map 0 Answers

Orbit Collision Axis 0 Answers

Planetoid Gravity 1 Answer

Rotating around to a point 1 Answer

help with my orbit camera script 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