CryptRaider Lesson 83 Problem: Wall stuttering instead of going down

I’ve been stuck in this lesson for a while, saw that even if I followed what the video says (changing the wall properties to movable, changing the moveOffSet of -600 and etc…), my wall instead of moving down starts stuttering up and down. I found a similar problem on the Udemy’s questions, but it didn’t fix the problem for me, here my code for the mover:

Mover.cpp:

#include "Mover.h"

#include "Math/UnrealMathUtility.h"

UMover::UMover()

{
    PrimaryComponentTick.bCanEverTick = true;
}

void UMover::BeginPlay()
{
    Super::BeginPlay();
    originalLocation = GetOwner()->GetActorLocation();
}

void UMover::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
    FVector currentLocation = GetOwner()->GetActorLocation();
    FVector targetLocation = originalLocation + moveOffSet;
    float speed = FVector::Distance(currentLocation, targetLocation) / moveTime;

    FVector newLocation = FMath::VInterpConstantTo(originalLocation, targetLocation, DeltaTime, speed);
    GetOwner()->SetActorLocation(newLocation);
}

Mover.h:

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Mover.generated.h"

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )

class CRYPTRAIDER_API UMover : public UActorComponent
{
    GENERATED_BODY()

public:
    UMover();

protected:
    virtual void BeginPlay() override;

public:
    virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

private:
    UPROPERTY(EditAnywhere)
    FVector moveOffSet;

    UPROPERTY(EditAnywhere)
    float moveTime = 4;

    UPROPERTY(EditAnywhere)
    bool shouldMove = false;

    FVector originalLocation;
};

I’m in version 5.3, I also tried to see if it was a collision thing, but the same problem happens if I put the wall in the sky far away from other objects (I’m using the SM_Dungeon_Wall_Decorative_D probably doesn’t matter, because the same problem happens with any other object).
Anyway thank for the help in advance!

Why you not if checking this otherwise your always setting the target everyframe.

if should move then you want to set the target location.

You mean like this?

    if(shouldMove)
    {
        FVector currentLocation = GetOwner()->GetActorLocation();
        FVector targetLocation = originalLocation + moveOffSet;
        float speed = FVector::Distance(currentLocation, targetLocation) / moveTime;

        FVector newLocation = FMath::VInterpConstantTo(originalLocation, targetLocation, DeltaTime, speed);
        GetOwner()->SetActorLocation(newLocation);
    }

If yes, it still stutter if I put “shouldMove” as true.

Update: I tried to use the Function “VInterpTo()” instead of “VInterpConstantTo()”, and it actually manages to move! Tho the problem is that it teleport so there is no animation while it does so. I guess I could continue with the course like this, but I would still like to know how to fix this WITH the function VInterpConstantTo() in case my future game needs it.

Your issue looks to be related to your newLocation variable. You’re setting it to be moving from the originalLocation to the targetLocation; however, you need it instead to move from the currentLocation. Every frame you start moving from the original location and then move a little, then keep doing the same thing. You want the current location to be your starting point when moving.

Something like this:

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

Additionally, @LaniganDev was stating to put your targetLocation variable set into an if statement such that you only need to set it when moving is set to true:

Something like this:

FVector TargetLocation = OriginalLocation;

if (ShouldMove)
{
    TargetLocation = OriginalLocation + MoveOffset;
}	
1 Like

Ok I tried to do what you said and it worked, even tho I feel kinda dumb saw that the solution was this simple ahahah! And yeah, I already did the if statement and it works as well.
Anyway thanks for the help!

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

Privacy & Terms