Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by nicho247 · Sep 12, 2015 at 10:02 PM · c#transform.rotatearound

Help in C# rotatearound is not around center object

Hi There,

I have a gameobject (B) rotating and rotatingaround another gameobject (A). It is working perfectly. Note A is not moving and B is moving. I have taken this code from (B) and applied it to another gameobject (C). So, gameobject (C) is rotating and rotating around another gameobject (B). Though it works, it is not working as intended. Note B is moving and C is moving. C appears to drift away from B (or vice versa), though B should remain at the center of the rotation axis of C.

I suspect I have introduced some sort of relative positioning snafu or I using the wrong function. I have searched around the forum and are confused with when/how to use quaternion slerp (might be the correct way, just unsure).

Code below.

 public class Moon : MonoBehaviour 
 {
     public float MoonRotation = -0.03660100f;
     public float MoonOrbitSpeed = 0.03660100f;
     public GameObject ObjectEarth;
 
     void Update () 
     {
         Vector3 MoonOrbitPoint = ObjectEarth.transform.position;
         transform.Rotate (new Vector3 (0.0f,MoonRotation*Time.smoothDeltaTime*360.0f,0.0f));
         transform.RotateAround (MoonOrbitPoint,new Vector3(0,1,0),MoonOrbitSpeed*Time.smoothDeltaTime*360.0f);
     }
 }
 

Thanks Nick

Comment
Add comment · Show 4
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 hexagonius · Sep 12, 2015 at 10:25 PM 0
Share

How is the parent child relation? I'd say ever Gameobject should be child of the one it rotates around. If you don't want this cascade you need to keep the distance to the reference gameobject in the code before rotating

avatar image nicho247 hexagonius · Sep 12, 2015 at 10:55 PM 0
Share

Hi hexagonius - good question. I have left the hierarchy not structured. Everything is flat. I suppose I can setup the structure, however this led to other types of problems (i am an obvious novice here). $$anonymous$$y scale, speeds, and distances got messed up quickly (and orbital centers).

In the code above, how would I keep the distance to the reference gameobject in the code before rotating? I only ask because I thought that is what I had done. Could you possibly update/correct the code above or elaborate what is out of sequence?

Separately, is the structuring is best practice? I suppose I should aim to $$anonymous$$imize bad habits.

avatar image Glurth nicho247 · Sep 13, 2015 at 01:04 AM 0
Share

Separately, is the structuring is best practice?

I like it, and your example is a great reason why. When you want to rotate around the parent (planet), you rotate around your child object's (moon) model-space(localPosition) origin (0,0,0). Now lets say you want to rotate the parent object (planet) around ITS parent(sun). No problem: rotate around your child object's(planet) model-space origin (0,0,0). Everything is going to rotate around only it's parent, and automatically include its parent's movement. There is no need to do any kind of vector subtractions or nested rotations for each "level".

Show more comments

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.

Follow this Question

Answers Answers and Comments

29 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

Related Questions

converting degrees per second into speed 1 Answer

Changing Variables on one script with multiple other scripts 1 Answer

Convert a java script in a c#??? thank you 1 Answer

Viewing in-game on Android Device 0 Answers

Shooting exactly to the center of screen 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