Advertisement
Guest User

click me if you want

a guest
Apr 8th, 2020
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "MarineNavMovementComponent.h"
  5. #include "Components/CapsuleComponent.h"
  6. #include "Engine/World.h"
  7.  
  8. void UMarineNavMovementComponent::BeginPlay() {
  9.     Super::BeginPlay();
  10.     CapsuleComponent = GetOwner()->FindComponentByClass<UCapsuleComponent>();
  11.     if (!CapsuleComponent) {
  12.         UE_LOG(LogTemp, Warning, TEXT("CapsuleComponent Not Found!!"));
  13.     }
  14. }
  15.  
  16. void UMarineNavMovementComponent::RequestDirectMove(const FVector& MoveVelocity, bool bForceMaxSpeed) {
  17.     Super::RequestDirectMove(MoveVelocity, bForceMaxSpeed);
  18.  
  19.     Direction = MoveVelocity.GetSafeNormal();
  20.  
  21.     FVector NextLocation = CalculateNextPositionByNav(Direction);
  22.     NextLocation = SetPositionToGround(NextLocation);
  23.     GetOwner()->SetActorLocation(NextLocation);
  24.  
  25.     FRotator TargetRotator = CalculateInterpRotateToDirection(Direction);
  26.     FRotator DiffRotation = Direction.Rotation() - TargetRotator;
  27.     if (FMath::Abs(DiffRotation.Yaw) < 0.01) {
  28.         UE_LOG(LogTemp, Warning, TEXT("DoSomething"));
  29.     }
  30.     GetOwner()->SetActorRotation(TargetRotator);
  31. }
  32.  
  33. FVector UMarineNavMovementComponent::GetDirection() const
  34. {
  35.     return Direction;
  36. }
  37.  
  38. void UMarineNavMovementComponent::ReceiveDamage()
  39. {
  40.     TestNotifyEvent();
  41. }
  42.  
  43. FVector UMarineNavMovementComponent::CalculateNextPositionByNav(const FVector& DirectionToMove)
  44. {
  45.     float DeltaTime = GetWorld()->GetDeltaSeconds();
  46.     FVector CurrentLocation = GetOwner()->GetActorLocation();
  47.     return   CurrentLocation + DirectionToMove * MoveSpeed * DeltaTime;
  48. }
  49.  
  50. FRotator UMarineNavMovementComponent::CalculateInterpRotateToDirection(const FVector& DirectionToRotate)
  51. {
  52.     FRotator Result = FMath::RInterpTo(GetOwner()->GetActorRotation(), DirectionToRotate.Rotation(), GetWorld()->GetDeltaSeconds(), RotateSpeed);
  53.  
  54.     Result.Roll = 0;
  55.     Result.Pitch = 0;
  56.     return Result;
  57. }
  58.  
  59. FVector UMarineNavMovementComponent::SetPositionToGround(FVector& Location)
  60. {
  61.     FHitResult Hit;
  62.     float CapsuleHalfHigh = GetCapsuleHalfHigh();
  63.     FVector FootLocation = Location + FVector::DownVector * CapsuleHalfHigh;
  64.     FVector End = FootLocation + FVector::DownVector * LineTraceToCheckGround;
  65.     FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());
  66.  
  67.     if (GetWorld()->LineTraceSingleByObjectType(Hit, Location, End, ObjectTypes, TraceParams)) {
  68.  
  69.         Location.Z = Hit.Location.Z + CapsuleHalfHigh + OffsetBetweenGround;
  70.     }
  71.     return Location;
  72. }
  73.  
  74. float UMarineNavMovementComponent::GetCapsuleHalfHigh()
  75. {
  76.     return CapsuleComponent->GetScaledCapsuleHalfHeight();
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement