How to get objects orbit around another object?

Hello, I am doing an EPQ, it is basicly re creating the solar system in UE4, basically what I need guidence on is how to get an object to orbit another object that is also obiting around anorther object. For instance i need the moon to orbit the Earth that is also orbiting the sun. I know it is possible but im just not sure how to do it. I am new to all of this and i have looked at youtube videos but none of those are working for me.

I am not sure the best way but my first idea to try, is to make all the orbiting bodies separate actors and use the AttachTo node on BeginPlay in the level blueprint. If I remember right, AttachTo can be set to lock the locations absolutely or relatively (use absolutely),
and can allow relative rotations.

My idea is to add a rotationMovement component to each actor, and if it has the option, set the pivot of the rotation offset by however far away the body is from the other body it is revolving around, so that the pivot centers on that body. and use AttachTo so the pivot stays stuck on that other body while allowing the attached child to do its own relative rotations.

There is probably a much better way to do this but that’s my first idea to try.
If you also need the revolving bodies to have their own spin/rotation then i am not sure if my approach will work. you would probably have to add more scene components to create abother layer of transform or just do the orbit vector math yourself in the worst case scenario

Okay I’ll try that, thank you for your responce!

i had some time so i made a mock up that work as seen below. the idea i used was to have a chain of planets via parenting. another important thing to note is that i left the scene root in so that i could have more control over the rotation or orbit and the distance from the parent. the root is the point the planet rotates around and the root also gets set to the location of the parent which makes things super easy to setup.

it includes a system for parenting to another planet (i was thinking sun, planets, and moons), this is based on a public variable so you can set it in the level to make things easier. so the parenting basically just attaches the new planet to the mesh of the parent one via a attach to component.

To control the rotation speed i got the rotating movement component and set the rotation rate based on another public variable so again can be set via the details panel with the planet selected in the level.

the last part of the script just controls how far from the root the planets mesh is. this is like controlling how far from the parent planet the orbit is.

so i just did something dumb and tried to use this to create a scale model of the universe, basically all planets 1/10,000 scale (in kilometers). yup the universe is freaking huge. i never really though how huge until now. mercury was to be 5800uu from the world origin and would have a diameter of 0.48uu. pluto would be 591,300 units from origin and be 0.237 units in diameter. even with the planets set to 100uu you can only see out to mars.

Well @ThompsonN13 actually works and is tested and proven. I would go with his way if I were you :slight_smile:

i actually made a change to that as well, i added another variable that sets the scale of the static mesh on its own. this eliminated the problem where if you set the actor scale it would also affect the distance from rotation center and possibly the children of the actor. i should also note that with the system i showed the only reason you would need to use the parenting is if you want to parent to a sun or more likely in the case of moons.

Okay, I’m in the process of doing it now, may I ask what is the target static mesh node that connects to the AttachToComponent? and the set node where the rotation rate connects to, and also what is the rotating movement node? thank you for all your help.

the target static mesh is basically just getting the mesh of the actor we have set to the variable. if you look at the above picture you will see that i have the variable selected, then look at the details panel and see that the variable type is set to the class planet (thats the class we are currently working in), and last in the top left you will see that there is a static mesh in the component section. so basically the variable is just a reference to another planet in the scene and the script gets that actor then gets its static mesh. we do this so that when we attach the current planet it attaches to the location of the other planet and not the others root. if we attached to the other actors root then both planets would have the same root location which wouldnt work for a moon, basically it would look like where the roots were is where the sun would be if that makes sense.

the set node is set rotation rate which is part of the rotating movement component and the rotating movement node you see is a reference to the rotating movement component. to get this setup you just need to go to the components section and add new → rotating movement component, then select this component and drag it into the graph which will create the blue node you see. then drag off of rotating movement node you just created and search for set rotation rate which is the purple set node you see (this option just sets how fast the actors rotates around its origin, aka its orbit speed).