KingKerel
(KingKerel)
November 30, 2023, 9:45pm
1
I am still learning C++, and cant figure out a simple move script.
I made an actor that consists of two cubes stacked on each other.
I would like the top one, a blue cube, to spiral outward while also rotating itself.
How would I code such a thing?
I made several attempts, but just dont know how to implement this in C++.
From my Header file:
protected:
UPROPERTY(EditAnywhere)
float speed = 2.0f;
UPROPERTY(EditAnywhere)
float RotationSpeed = 200.0f;
FVector StartPos;
FVector CurPosition;
FVector StartRotation;
FRotator CurRotation;
float distance;
From my C Plus Plus file:
void UMySceneComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
distance += DeltaTime * speed;
CurRotation.Yaw += DeltaTime * RotationSpeed;
CurPosition += CurPosition.ForwardVector * distance;
SetRelativeLocation(CurPosition);
SetRelativeRotation(CurRotation);
}
KingKerel
(KingKerel)
December 1, 2023, 2:20pm
2
So the good news is I got it to spiral, and I got the cube to rotate.
However, if I now rotate the actor “BP_TestCube” during gameplay, the blue cube will take on a really weird orbit.
Does anybody have an idea of how to fix it?
New header file:
protected:
UPROPERTY(EditAnywhere)
float Speed = 25.0f;
UPROPERTY(EditAnywhere)
float OrbitSpeed = 100.0f;
UPROPERTY(EditAnywhere)
float RotationSpeed = 200.0f;
float Distance;
FVector StartPos;
FVector CurPosition;
FRotator OrbitRotation;
FRotator ObjectRotation;
New C Plus Plus file:
void UMySceneComponent::BeginPlay()
{
Super::BeginPlay();
StartPos = this->GetRelativeLocation();
OrbitRotation = this->GetRelativeRotation();
ObjectRotation = this->GetRelativeRotation();
}
void UMySceneComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
OrbitRotation.Yaw += DeltaTime * OrbitSpeed;
SetRelativeRotation(OrbitRotation);
Distance += DeltaTime * Speed;
CurPosition = StartPos;
CurPosition += this->GetForwardVector() * Distance;
SetRelativeLocation(CurPosition);
ObjectRotation.Yaw += DeltaTime * RotationSpeed;
SetRelativeRotation(ObjectRotation);
}