Advertisement
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 "MarineNavMovementComponent.h"
- #include "Components/CapsuleComponent.h"
- #include "Engine/World.h"
- void UMarineNavMovementComponent::BeginPlay() {
- Super::BeginPlay();
- CapsuleComponent = GetOwner()->FindComponentByClass<UCapsuleComponent>();
- if (!CapsuleComponent) {
- UE_LOG(LogTemp, Warning, TEXT("CapsuleComponent Not Found!!"));
- }
- }
- void UMarineNavMovementComponent::RequestDirectMove(const FVector& MoveVelocity, bool bForceMaxSpeed) {
- Super::RequestDirectMove(MoveVelocity, bForceMaxSpeed);
- Direction = MoveVelocity.GetSafeNormal();
- FVector NextLocation = CalculateNextPositionByNav(Direction);
- NextLocation = SetPositionToGround(NextLocation);
- GetOwner()->SetActorLocation(NextLocation);
- FRotator TargetRotator = CalculateInterpRotateToDirection(Direction);
- FRotator DiffRotation = Direction.Rotation() - TargetRotator;
- if (FMath::Abs(DiffRotation.Yaw) < 0.01) {
- UE_LOG(LogTemp, Warning, TEXT("DoSomething"));
- }
- GetOwner()->SetActorRotation(TargetRotator);
- }
- FVector UMarineNavMovementComponent::GetDirection() const
- {
- return Direction;
- }
- void UMarineNavMovementComponent::ReceiveDamage()
- {
- TestNotifyEvent();
- }
- FVector UMarineNavMovementComponent::CalculateNextPositionByNav(const FVector& DirectionToMove)
- {
- float DeltaTime = GetWorld()->GetDeltaSeconds();
- FVector CurrentLocation = GetOwner()->GetActorLocation();
- return CurrentLocation + DirectionToMove * MoveSpeed * DeltaTime;
- }
- FRotator UMarineNavMovementComponent::CalculateInterpRotateToDirection(const FVector& DirectionToRotate)
- {
- FRotator Result = FMath::RInterpTo(GetOwner()->GetActorRotation(), DirectionToRotate.Rotation(), GetWorld()->GetDeltaSeconds(), RotateSpeed);
- Result.Roll = 0;
- Result.Pitch = 0;
- return Result;
- }
- FVector UMarineNavMovementComponent::SetPositionToGround(FVector& Location)
- {
- FHitResult Hit;
- float CapsuleHalfHigh = GetCapsuleHalfHigh();
- FVector FootLocation = Location + FVector::DownVector * CapsuleHalfHigh;
- FVector End = FootLocation + FVector::DownVector * LineTraceToCheckGround;
- FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());
- if (GetWorld()->LineTraceSingleByObjectType(Hit, Location, End, ObjectTypes, TraceParams)) {
- Location.Z = Hit.Location.Z + CapsuleHalfHigh + OffsetBetweenGround;
- }
- return Location;
- }
- float UMarineNavMovementComponent::GetCapsuleHalfHigh()
- {
- return CapsuleComponent->GetScaledCapsuleHalfHeight();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement