(Solved) TArray Platform Target Positions Incorrect

I setup a simple TArray of TargetLocations and have been able to cycle through the Array successfully. However, the positions are wrong. TargetLocation[0] is correct, TargetLocation[1] is correct, after that subsequent TargetLocations are incorrect.
I’m not going to copy the whole code, just my changes from the lessons:

        if (JourneyTravelled > JourneyLength)
        {
            /*             FVector Swap = GlobalStartLocation;
            GlobalStartLocation = GlobalTargetLocation;
            GlobalTargetLocation = Swap;
 */
            GlobalStartLocation = GlobalTargetLocation;
            if (TargetLocationIndex == TargetLocation.Num() - 1)
            {
                TargetLocationIndex = 0;
            }
            else
            {
                TargetLocationIndex += 1;
            }
            GlobalTargetLocation = GetTransform().TransformPosition(TargetLocation[TargetLocationIndex]);
            //GlobalTargetLocation = TargetLocation[TargetLocationIndex];
        }

Where am I going wrong? The TargetLocation is determined globally not locally.

Please help.

TargetLocation[0] = GetActorLocation() in BeginPlay, the platform location in world space
TargetLocation[1] is in local space in relation to initial spawn location (world space) of the platform
TargetLocation[n] should follow the same concept, where each additional TargetLocation is in local space relative to the initial spawn location of the platform.

Therefore, converting each TargetLocation[n] to world space via GetTransform().TransformPosition(TargetLocation[n]) should place the TargetLocation at its world position relative to its local position. Essentially the same position, just in different spaces. One relative to world origin, the other relative to platform origin.

However, this is not happening. TargetLocations beyond TargetLocation[1] are not where they should be. They are in very different locations as the platform moves from index to index in the array.

I’ve tried adding, subtracting, and multiplying various FVectors. I can get close, but never quite there. Close meaning the path is correct, but the positions are all wrong.

Any thoughts? Anyone?

SOLVED:
My premise was correct, TargetLocation[index] is local to the initial platform location. Initial is key here. GetTransform() gets the transform at the moment of it being called. So if you call GetTransform() on the Platform at anytime after BeginPlay(), the values will be different. What was happening in my above presented code, is that I was correctly getting the local to world TargetLocation of the Platform; however, the Platform was not in the same place. So TargetLocation was local to the Platforms new world location, not the original spawn location where all TargetLocations were built upon.

The Fix: Save the Platforms initial transform and location. Calculate each TargetLocation from that initial location. As seen below.

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

#include "MovingPlatform.h"

AMovingPlatform::AMovingPlatform()
{
    PrimaryActorTick.bCanEverTick = true;
    SetMobility(EComponentMobility::Movable);
}

void AMovingPlatform::BeginPlay()
{
    Super::BeginPlay();

    if (HasAuthority())
    {
        SetReplicates(true);
        SetReplicateMovement(true);
    }

    GlobalStartLocation = GetActorLocation();
    Platform = GetTransform();
    PlatformStart = Platform.GetLocation();
    GlobalTargetLocation = GetTransform().TransformPosition(TargetLocation[0]);
}

void AMovingPlatform::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    if (HasAuthority())
    {
        FVector Location = GetActorLocation();

        float JourneyLength = (GlobalTargetLocation - GlobalStartLocation).Size();
        float JourneyTravelled = (Location - GlobalStartLocation).Size();

        if (JourneyTravelled > JourneyLength)
        {
            /*             FVector Swap = GlobalStartLocation;
            GlobalStartLocation = GlobalTargetLocation;
            GlobalTargetLocation = Swap;
 */
            GlobalStartLocation = GlobalTargetLocation;
            if (TargetLocationIndex == TargetLocation.Num())
            {
                TargetLocationIndex = 0;
                GlobalTargetLocation = PlatformStart;
            }
            else
            {
                GlobalTargetLocation = Platform.TransformPosition(TargetLocation[TargetLocationIndex]);
                TargetLocationIndex += 1;
            }
        }

        FVector MoveDirection = (GlobalTargetLocation - GlobalStartLocation).GetSafeNormal();

        Location += PlatformVelocity * DeltaTime * MoveDirection;
        SetActorLocation(Location);
    }
}

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

Privacy & Terms