Guest User

MoverComponent.cpp

a guest
May 22nd, 2026
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "MoverComponent.h"
  5. #include "Math/UnrealMathUtility.h"
  6.  
  7.  
  8. UMoverComponent::UMoverComponent()
  9. {
  10.     PrimaryComponentTick.bCanEverTick = true;
  11.  
  12. }
  13.  
  14.  
  15. // Called when the game starts
  16. void UMoverComponent::BeginPlay()
  17. {
  18.     Super::BeginPlay();
  19.  
  20.     Owner = GetOwner();
  21.  
  22.     if (bMoveOnlyMeshComponent)
  23.     {
  24.         MeshComponent = Owner->FindComponentByClass<UStaticMeshComponent>();
  25.    
  26.         if (MeshComponent)
  27.         {
  28.             StartLocation = MeshComponent->GetComponentLocation();
  29.         }
  30.         else
  31.         {
  32.             bMoveOnlyMeshComponent = false;
  33.         }
  34.  
  35.     }
  36.  
  37.     if(!bMoveOnlyMeshComponent)
  38.     {
  39.         StartLocation = Owner->GetActorLocation();
  40.     }
  41.  
  42.     TargetLocation = StartLocation;
  43. }
  44.  
  45.  
  46. // Called every frame
  47. void UMoverComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  48. {
  49.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  50.  
  51.     RecalculateTarget();
  52.  
  53.     if (bMoveOnlyMeshComponent)
  54.     {
  55.         FVector CurrentLocation = MeshComponent->GetComponentLocation();
  56.         if (!CurrentLocation.Equals(TargetLocation))
  57.         {
  58.             MeshComponent->SetWorldLocation(FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, MovingSpeed));
  59.         }
  60.     }
  61.     else
  62.     {
  63.         FVector CurrentLocation = Owner->GetActorLocation();
  64.  
  65.         if (!CurrentLocation.Equals(TargetLocation))
  66.         {
  67.             Owner->SetActorLocation(FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, MovingSpeed));
  68.         }
  69.     }
  70. }
  71.  
  72. void UMoverComponent::Trigger(bool bNewState)
  73. {
  74.     bOpen = bNewState;
  75. }
  76.  
  77. void UMoverComponent::RecalculateTarget()
  78. {
  79.     if (bOpen != bOpenPrevState)
  80.     {
  81.         bOpenPrevState = bOpen;
  82.  
  83.         if (bOpen)
  84.         {
  85.             TargetLocation = StartLocation + MoveDirection.GetSafeNormal() * MaxTravelDist;
  86.         }
  87.         else
  88.         {
  89.             TargetLocation = StartLocation;
  90.         }
  91.     }
  92. }
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment