This component is similar to RotatingMovementComponent
in that it is designed to make the components parented to it move in a particular way. In this instance, it will move any attached components in an orbit around a fixed point at a fixed distance.
This could be used, for example, for a shield that orbits around a character in an Action RPG.
SceneComponent
subclass and add the following properties to the class declaration:UPROPERTY() bool RotateToFaceOutwards; UPROPERTY() float RotationSpeed; UPROPERTY() float OrbitDistance; float CurrentValue;
RotationSpeed = 5; OrbitDistance = 100; CurrentValue = 0; RotateToFaceOutwards = true;
TickComponent
function:float CurrentValueInRadians = FMath::DegreesToRadians<float>(CurrentValue); SetRelativeLocation(FVector(OrbitDistance * FMath::Cos(CurrentValueInRadians), OrbitDistance * FMath::Sin(CurrentValueInRadians), RelativeLocation.Z)); if (RotateToFaceOutwards) { FVector LookDir = (RelativeLocation).GetSafeNormal(); FRotator LookAtRot = LookDir.Rotation(); SetRelativeRotation(LookAtRot); } CurrentValue = FMath::Fmod(CurrentValue + (RotationSpeed* DeltaTime) ,360);
#pragma once #include "Components/SceneComponent.h" #include "OrbitingMovementComponent.generated.h" UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) class UE4COOKBOOK_API UOrbitingMovementComponent : public USceneComponent { GENERATED_BODY() public: // Sets default values for this component's properties UOrbitingMovementComponent(); // Called when the game starts virtual void BeginPlay() override; // Called every frame virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override; UPROPERTY() bool RotateToFaceOutwards; UPROPERTY() float RotationSpeed; UPROPERTY() float OrbitDistance; float CurrentValue; }; #include "UE4Cookbook.h" #include "OrbitingMovementComponent.h" // Sets default values for this component's properties UOrbitingMovementComponent::UOrbitingMovementComponent() { // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features // off to improve performance if you don't need them. bWantsBeginPlay = true; PrimaryComponentTick.bCanEverTick = true; RotationSpeed = 5; OrbitDistance = 100; CurrentValue = 0; RotateToFaceOutwards = true; //... } // Called when the game starts void UOrbitingMovementComponent::BeginPlay() { Super::BeginPlay(); //... } // Called every frame void UOrbitingMovementComponent::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) { Super::TickComponent( DeltaTime, TickType, ThisTickFunction ); float CurrentValueInRadians = FMath::DegreesToRadians<float>(CurrentValue); SetRelativeLocation( FVector(OrbitDistance * FMath::Cos(CurrentValueInRadians), OrbitDistance * FMath::Sin(CurrentValueInRadians), RelativeLocation.Z)); if (RotateToFaceOutwards) { FVector LookDir = (RelativeLocation).GetSafeNormal(); FRotator LookAtRot = LookDir.Rotation(); SetRelativeRotation(LookAtRot); } CurrentValue = FMath::Fmod(CurrentValue + (RotationSpeed* DeltaTime) ,360); //... }
Actor
Blueprint.OrbitingMovement
Component to your Actor
, then add a few meshes using the Cube
component. Parent them to the OrbitingMovement
component by dragging them on to it in the Components panel. The resulting hierarchy should look like the following:Actor
.RotateToFaceOutwards
specifies whether the component will orient to face away from the center of rotation on every update. RotationSpeed
is the number of degrees the component rotates every second.OrbitDistance
indicates the distance that the components that rotate must be moved from the origin. CurrentValue
is the current rotation position in degrees.TickComponent
function, we calculate the location and rotation of our component.DegreesToRadians
function to convert our current value in degrees to radians.SetRelativeLocation
function uses the general equation for circular motion, that is—Pos(θ) = cos(θ in radians), sin(θ in radians). We preserve the Z axis position of each object.RotateToFaceOutwards
is true
, and involves getting the relative offset of the component to its parent, and creating a rotator based on a vector pointing from the parent to the current relative offset. We then set the relative rotation to the resulting rotator.RotationSpeed
units per second, clamping the resulting value between 0 and 360 to allow the rotation to loop.18.119.157.39