Rotate actor around player

Hi!

I’m trying to implement this piece of code from Rotate Actor Around player:

#include "RotateActorAroundPlayer.h"


// Sets default values
ARotateActorAroundPlayer::ARotateActorAroundPlayer()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	Dimensions = FVector (300, 0, 0);
	AxisVector = FVector (0, 0, 1);
	Multiplier = 50.f;
}

// Called when the game starts or when spawned
void ARotateActorAroundPlayer::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ARotateActorAroundPlayer::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	FVector NewLocation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();
			
	AngleAxis += DeltaTime * Multiplier;

	if(AngleAxis >= 360.0f) 
	{
		AngleAxis = 0;
	}

	FVector myCharacter = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();

	FVector RotateValue = Dimensions.RotateAngleAxis(AngleAxis, AxisVector);

	NewLocation.X += RotateValue.X;
	NewLocation.Y += RotateValue.Y;
	NewLocation.Z += RotateValue.Z;

	SetActorLocation(NewLocation, false, 0, ETeleportType::None);

}

In blueprints:

The object moves around the player but it doesn’t rotate. I don’t know what I’ve made wrong. Probably the Bluescript function RotateVectorAroundAxis is not the same function for RotateAngleAxis in C++.

I have used the same values for Dimensions, AxisVector and Multiplier.

What have I done wrong?

Update:

I have tested the C++ code and it doesn’t the same: the RotateActorAroundPlayer moves around the player but it doesn’t face to the player all the time.

Hi! It is not working because you forget to set the actor rotation. I see only calling of SetActorLocation functions in both c++ and BP. You need to add SetActorRotation function.

3 Likes

The incredible thing is that in the video of the article it works without using rotation.

Thanks a lot! It works like a charm!!

In the video, they use Rotation And Location.

But in the Github and in the tutorial there isn’t any mention to rotation. I watched the video once, and then I have supposed that everything else was like the video.

1 Like

They tricked you! :smiley: Trust no one :wink:

Yes. Thanks you both are here.

Thanks a lot for your help.