Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "MoverComponent.h"
- #include "Math/UnrealMathUtility.h"
- UMoverComponent::UMoverComponent()
- {
- PrimaryComponentTick.bCanEverTick = true;
- }
- // Called when the game starts
- void UMoverComponent::BeginPlay()
- {
- Super::BeginPlay();
- Owner = GetOwner();
- if (bMoveOnlyMeshComponent)
- {
- MeshComponent = Owner->FindComponentByClass<UStaticMeshComponent>();
- if (MeshComponent)
- {
- StartLocation = MeshComponent->GetComponentLocation();
- }
- else
- {
- bMoveOnlyMeshComponent = false;
- }
- }
- if(!bMoveOnlyMeshComponent)
- {
- StartLocation = Owner->GetActorLocation();
- }
- TargetLocation = StartLocation;
- }
- // Called every frame
- void UMoverComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
- {
- Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
- RecalculateTarget();
- if (bMoveOnlyMeshComponent)
- {
- FVector CurrentLocation = MeshComponent->GetComponentLocation();
- if (!CurrentLocation.Equals(TargetLocation))
- {
- MeshComponent->SetWorldLocation(FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, MovingSpeed));
- }
- }
- else
- {
- FVector CurrentLocation = Owner->GetActorLocation();
- if (!CurrentLocation.Equals(TargetLocation))
- {
- Owner->SetActorLocation(FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, MovingSpeed));
- }
- }
- }
- void UMoverComponent::Trigger(bool bNewState)
- {
- bOpen = bNewState;
- }
- void UMoverComponent::RecalculateTarget()
- {
- if (bOpen != bOpenPrevState)
- {
- bOpenPrevState = bOpen;
- if (bOpen)
- {
- TargetLocation = StartLocation + MoveDirection.GetSafeNormal() * MaxTravelDist;
- }
- else
- {
- TargetLocation = StartLocation;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment