How do i rotate an component aroud another component?

So i was following one of the UE4 guides of c++ programming, and one of the “do it yourself” part is to create a component that orbits another.

I want to rotate an component (a simple static mesh) around another component (that is also the root of the actor).

I set this in the constructor. SphereComponent is the root:

OrbitSphereVisual->SetRelativeLocation(FVector(0.0f, 100.0f, 0.0f)); 

An this in the Event Tick:

Rotate += DeltaTime * 0.1;
	if (Rotate > 360)
		Rotate = 1;

	OrbitSphereVisual->SetRelativeLocation(OrbitSphereVisual->GetComponentLocation().RotateAngleAxis(Rotate, FVector(0, 0, 1)));

But nothing happens as it seens. What i’m doing wrong, or what did i get wrong?



a few thing may make your sphere not behave the way you want.

  • You do this every frame, so you don’t have to calculate the Rotate, what you want is to multiply Delta time with a speed.

  • You want to locally rotate your object, yet you take it’s world location (GetComponentLocation()), that you feed back again as a local location. You have to get the local location using GetRelativeLocation().

     OrbitSphereVisual->SetRelativeLocation(OrbitSphereVisual->GetRelativeLocation().RotateAngleAxis(DeltaTime * Speed, FVector(0, 0, 1)));