What's the correct way to reverse the moving?

My current code is:

if(ShouldMove == true)

{

FVector CurrentLocation = GetOwner() -> GetActorLocation();

FVector TargetLocation = OriginalLocation + MoveOffset;

float Speed = FVector::Distance(OriginalLocation, TargetLocation) / MoveTime;

FVector NewLocation = FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, Speed);

GetOwner() -> SetActorLocation(NewLocation);

}

else

{

    FVector CurrentLocation = GetOwner() -> GetActorLocation();

   

    float Speed = FVector::Distance(CurrentLocation, OriginalLocation) / MoveTime;

    FVector NewLocation = FMath::VInterpConstantTo(CurrentLocation, OriginalLocation, DeltaTime, Speed);

    GetOwner() -> SetActorLocation(NewLocation);

}

This works however the wall slows down as it goes back up. I’m guessing the distance is recalculated each time, instead of there being a unaffected variable for current location like there is for OriginalLocation.

Would I need to set a new variable outside the tick function such Speed?

Are you using the editor to check/uncheck ShouldMove? Editing the properties will reinitialise the component and thus call BeginPlay again. You would need to modify that variable programatically (done later)


P.S. Having a space between -> is rather unusual and I only see beginners of C++ do that. It would be like having a space after .; as you said in your other thread that you have used Java before, would you write

SomeObject . SomeFunc();

or does that seem weird?

Yes, I am. I guess I got too curious and jumped the gun. I’ll wait till I reach that part haha.

Yeah, not sure why I did that, maybe a remnant of my PowerShell scripting job I had last year. Also maybe because I was taking the word pointer too literally and making it look like an arrow haha.

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

Privacy & Terms