making actor class created in c++ move towards its mesh direction using the details transform

Hi guys :slight_smile:

This is my first topic so i hope i’ll do it right.

Following a udemy course I’ve created an actor c++ class called MovingPlatform which moves forward an backwards using velocity and distance members.

Then after compiling the class and creating a blueprint class based on that actor, I’ve added a cube mesh.

The mesh is moving as expected but I cannot set the direction of the movement to follow the mesh’s forward vector seen by the gizmo.

If im using the relative view i can only see the cube rotating, and if I’m changing to the world relative view, I can see it’s vector’s direction changing too, but the actor’s vector stays the same, and the cube is moving in the actors direction.

Hope i made my point clear

thanks guys

Can you share the code and perhaps a visual of the issue?

sure thank you for your reply.

header file:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MovingPlatform.generated.h"

UCLASS()
class  AMovingPlatform : public AActor
{
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AMovingPlatform();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

private:

	UPROPERTY(EditAnywhere, Category = "Moving")
		FVector PlatformVelocity = FVector(100, 0, 0);
	UPROPERTY(EditAnywhere, Category = "Moving")
		float MoveDistance = 100;
	UPROPERTY(EditAnywhere, Category = "Rotation")
		FRotator RotationVelocity;

	FVector StartLocation;

	void MovePlatform(float DeltaTime);
	void RotatePlatform(float DeltaTime);

	bool ShouldPlatformReturn() const;
	float GetDistanceMoved() const;

};

source file:

// Fill out your copyright notice in the Description page of Project Settings.


#include "MovingPlatform.h"

// Sets default values
AMovingPlatform::AMovingPlatform()
{
	PrimaryActorTick.bCanEverTick = true;

}

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

	StartLocation = GetActorLocation();

	FString Name = GetName();

	UE_LOG(LogTemp, Display, TEXT("BeginPlay: %s"), *Name);
}

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

	MovePlatform(DeltaTime);
	RotatePlatform(DeltaTime);
}


void AMovingPlatform::MovePlatform(float DeltaTime)
{
	if (ShouldPlatformReturn())
	{
		FVector MoveDirection = PlatformVelocity.GetSafeNormal();
		StartLocation = StartLocation + MoveDirection * MoveDistance;
		SetActorLocation(StartLocation);
		PlatformVelocity = -PlatformVelocity;
	}
	else
	{
		FVector CurrentLocation = GetActorLocation();
		CurrentLocation = CurrentLocation + (PlatformVelocity * DeltaTime);
		SetActorLocation(CurrentLocation);
	}
}

void AMovingPlatform::RotatePlatform(float DeltaTime)
{
	FRotator CurrentRotation = GetActorRotation();
	CurrentRotation = CurrentRotation + RotationVelocity * DeltaTime;
	SetActorRotation(CurrentRotation);
}


bool AMovingPlatform::ShouldPlatformReturn() const
{
	return GetDistanceMoved() > MoveDistance;
}

float AMovingPlatform::GetDistanceMoved() const
{
	return FVector::Dist(StartLocation, GetActorLocation());
}

after I’ve compiled this I created a blueprint subclass from it, and attached a static mesh.

you can see in the photo that x axis points down in world view

world mode

and this in local mode where the x axis points towards right

local mode

when i start the simulation the mesh always moves relative to the local vector, and what i want to do is to set the rotation inside the editor so that the mesh move to the world direction

hope i made that clear :pray:

You images are inverted: the local mode image is actually world aligned. You can tell because the actors gizmo is aligned with the viewports that is on the bottom left and the globe icon is active:
image

image

So your actor is moving in world direction. What I understand you want is for the actor to move in the direction of its forward vector. You can do that like so:

void AMovingPlatform::MovePlatform(const float Delta)
{
	const FVector Offset = GetActorForwardVector() * Speed * Delta;
	AddActorWorldOffset(Offset);
}
1 Like

Thank you very much for your time and effort.

:slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.