How to rotate an actor around another actor? Like an orbit or something. What is the official way that unreal developers recommend doing this? I can’t use a spring arm btw.
It all depends, there are multiple ways of rotating. You asked for orbits so i assume you want rotate in one plane.
First if you want use physics:
- easy way is to use physics constraint component, allow angular movement, disallow one linear direction.
- harder way, you need to make some physic calculations, and apply external force that pushes object into orbit. Something like fake gravity, formula for force is trivial, but not that easy to find. You also may constrain movment to single plane. Normal gravity formula does not work quite well because speed force and distance to center all depend on one another, it is also very easy to create elliptic or hyperbolic orbits.
For objects you do not need to use physics for:
- create blueprint with scene root as axis or center of rotation. Then add static mesh (or any component) at some distance (this will be orbit distaance), then on event tick rotate that blueprint actor.
- bit more advanced: store game seconds at begin play in some variable (for eg in player controller), then create similar blueprint to above one, but do not bother with root and component distance. Instead calculate location and rotation of actor, and let it update self location on event tick. To get time again use game seconds (substract beginning game seconds from current). Easiest way to calculate location of object is to create vector for eg: [1,0,0] then multiply it by orbit distance, then use rotate vector around axis (for axis put [0,0,1]). And angle should be time* delta angle.
This is what I’m looking for, but how do you actually do the rotation?
Event Tick ----> Pawn.SetWorldPosition(RotateAroundMeshAtSomeDistance)
Here is my code so far, but it doesn’t do anything.
Your angle is 0, meaning no rotation will occur.
I tried setting a value, but now it rotates over a HUGE radius and for some reason there is an offset, meaning after it’s rotated once, it’s no longer where it originally was.
One thing you could try is finding the difference between A & B to get C, and then using rotate vector on C, and then setting the location of B to A + C. A being the thing you want to orbit around, B being the thing that will be orbiting.
I am not at my unreal pc, but i think there is yet another way for what you want:
- create blueprint
- add static mesh (or component you want to orbit)
- expose FLOAT variable to editor (small eye icon next to variable), lets name it DISTANCE
- in construction set LOCAL (or relative) location of static mesh as: [1,0,0] * DISTANCE
Above will set your mesh (or component) at correct distance from center of rotation
Now add rotation movement component (this part is what i am not sure and cannot verify without unreal)
It will have rotation speed, now you need to fill all values to have movement you like.
ps. rotation component has pivot offset exposed, so you may set its value to [1,0,0]*distance, instead of moving mesh.
What Jamendxman3 said, but use direction vectors for both InVect and Axis of the RotateVectorAroundAxis node.
Here’s one I made which puts OrbitingActorRef into orbit around the ThirdPersonChar.
I’m storing the direction vector because the orbit occasionally lagged behind when I referenced an external actor and did a live lookup.