UnrealScripterAI
Discord ↗
Link Copied to Clipboard
×
"Create a smooth movement animation system"
// Smooth Movement Animation System in Unreal // In your character's header file UCLASS() class YOURGAME_API AYourCharacter : public ACharacter { GENERATED_BODY() public: // Sets default values for this character's properties AYourCharacter(); // Called every frame virtual void Tick(float DeltaTime) override; protected: // Called when the game starts or when spawned virtual void BeginPlay() override; // Smooth movement function void SmoothMovement(float DeltaTime); // Movement speed UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") float MovementSpeed; // Target location UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") FVector TargetLocation; private: // Current location FVector CurrentLocation; // Lerp alpha float LerpAlpha; }; // In your character's source file AYourCharacter::AYourCharacter() { PrimaryActorTick.bCanEverTick = true; } void AYourCharacter::BeginPlay() { Super::BeginPlay(); CurrentLocation = GetActorLocation(); LerpAlpha = 0.0f; } void AYourCharacter::Tick(float DeltaTime) { Super::Tick(DeltaTime); SmoothMovement(DeltaTime); } void AYourCharacter::SmoothMovement(float DeltaTime) { if (LerpAlpha < 1.0f) { LerpAlpha = FMath::Clamp(LerpAlpha + DeltaTime * MovementSpeed, 0.0f, 1.0f); FVector NewLocation = FMath::Lerp(CurrentLocation, TargetLocation, LerpAlpha); SetActorLocation(NewLocation); } }