1
\$\begingroup\$

So basically, I want my platform to move first on the z axis from start position to end position, and then from end position to start again. Now after reaching start, it should change its direction to the y axis, repeat the same motion (start to end to start) and then change direction back to the z axis. But I can't seem to do it.

Can anyone please help me achieve this in UE5?

// Sets default values
AMovingPlatform::AMovingPlatform()
{
    // 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;
}
// Called when the game starts or when spawned
void AMovingPlatform::BeginPlay()
{
    Super::BeginPlay();
    StartLocation = GetActorLocation();  
    
    UE_LOG(LogTemp, Display, TEXT("Hello"));
}
// 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 {
        if (ShouldChangeDirection()) {
            PlatformVelocity = FVector(0, 600, 0); // Change direction
            FVector MoveDirection = PlatformVelocity.GetSafeNormal();
            StartLocation = StartLocation + MoveDirection * MoveDistance;
            SetActorLocation(StartLocation);
            PlatformVelocity = -PlatformVelocity;

        }
        FVector CurrentLocation = GetActorLocation();
        CurrentLocation = CurrentLocation + (PlatformVelocity * DeltaTime);
        SetActorLocation(CurrentLocation);
    }   
}
void AMovingPlatform::RotatePlatform(float DeltaTime) {
    
    AddActorLocalRotation(RotationVelocity * DeltaTime);

}

bool AMovingPlatform::ShouldChangeDirection() const {
    

    float Epsilon = 10.0f; // Adjust this value to account for floating-point precision
    return GetDistanceMoved() > MoveDistance - Epsilon;
    //return StartLocation == GetActorLocation();
}

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

float AMovingPlatform :: GetDistanceMoved() const {

    return FVector::Dist(StartLocation, GetActorLocation());
}
// 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 FAKEFALLGUYS_API 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;


    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Moving")
    FVector PlatformVelocity = FVector(0,0,500);

    UPROPERTY(EDITANYWHERE, BlueprintReadWrite, Category = "Moving")
    float MoveDistance = 500;
    UPROPERTY(EDITANYWHERE, BlueprintReadWrite, Category = "Rotation")
    FRotator RotationVelocity;
    FVector StartLocation;

    UFUNCTION(BlueprintCallable)
        void MovePlatform(float DeltaTime);

    UFUNCTION(BlueprintCallable)
        void RotatePlatform(float DeltaTime);

    UFUNCTION(BlueprintCallable)
        bool ShouldPlatformReturn() const;

    UFUNCTION(BlueprintCallable)
        float GetDistanceMoved() const;

    UFUNCTION(BlueprintCallable)
        bool ShouldChangeDirection() const;
private:

};
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Use a spline member object and have the platform follow it.

I recommend making the spline member object blueprint editable and viewable, defaulting to None, in order to make life easier during level editing/creation.

How to make a spline: https://www.youtube.com/watch?v=eKIiWa19EMI

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .