Advertisement
Guest User

VRCharactercpp-Lecture13

a guest
Apr 9th, 2020
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "VRCharacter.h"
  5. #include "Camera/CameraComponent.h"
  6. #include "Components/InputComponent.h"
  7. #include "Components/StaticMeshComponent.h"
  8. #include "GameFramework/PlayerController.h"
  9. #include "TimerManager.h"
  10. #include "Components/CapsuleComponent.h"
  11. #include "NavigationSystem.h"
  12.  
  13. // Sets default values
  14. AVRCharacter::AVRCharacter()
  15. {
  16.     // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  17.     PrimaryActorTick.bCanEverTick = true;
  18.  
  19.     VRFeet = CreateDefaultSubobject<USceneComponent>(TEXT("VRFeet"));
  20.     VRFeet->SetupAttachment(GetRootComponent());
  21.     VRFeet->SetRelativeLocation(FVector(0, 0, -88));
  22.  
  23.     VRCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("VRCamera"));
  24.     VRCamera->SetupAttachment(VRFeet);
  25.  
  26.     DestinationMarker = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("DestinatioNMarker"));
  27.     DestinationMarker->SetupAttachment(GetRootComponent());
  28.  
  29.  
  30.  
  31.     //CharacterMovement->MaxWalkSpeed = 300.0f;
  32. }
  33.  
  34. // Called when the game starts or when spawned
  35. void AVRCharacter::BeginPlay()
  36. {
  37.     Super::BeginPlay();
  38.     DestinationMarker->SetVisibility(false);
  39.     DestinationMarker->SetCollisionEnabled(ECollisionEnabled::NoCollision);
  40. }
  41.  
  42. // Called every frame
  43. void AVRCharacter::Tick(float DeltaTime)
  44. {
  45.     Super::Tick(DeltaTime);
  46.  
  47.     //Move VRCamera back to root so that VRCamera aligns with Character Capsule Collision.
  48.     FVector NewCameraOffset = VRCamera->GetComponentLocation() - GetActorLocation();
  49.     NewCameraOffset.Z = 0.0f;
  50.     AddActorWorldOffset(NewCameraOffset);
  51.     VRFeet->AddWorldOffset(-NewCameraOffset);
  52.  
  53.     UpdateDestinatonMarker();
  54.  
  55. }
  56.  
  57. // Called to bind functionality to input
  58. void AVRCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  59. {
  60.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  61.  
  62.     PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AVRCharacter::MoveForward);
  63.     PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AVRCharacter::MoveRight);
  64.     PlayerInputComponent->BindAction(TEXT("Teleport"), IE_Released, this, &AVRCharacter::BeginTeleport);
  65.  
  66. }
  67.  
  68. bool AVRCharacter::FindTeleportDestination(FVector &OutLocation)
  69. {
  70.  
  71.     FVector StartHit = VRCamera->GetComponentLocation();
  72.     FVector EndHit = StartHit + VRCamera->GetForwardVector() * MaxTeleportDistance;
  73.     FHitResult DestinationHit;
  74.  
  75.     UNavigationSystemV1* NavSystem = UNavigationSystemV1::GetCurrent(GetWorld());
  76.     if (!NavSystem) { UE_LOG(LogTemp, Warning, TEXT("No NavSystem")); return false; }
  77.     FNavLocation NavLocation;
  78.  
  79.     bool bValidHit = GetWorld()->LineTraceSingleByChannel(DestinationHit, StartHit, EndHit, ECC_Visibility);
  80.     bool bValidNavProjection = NavSystem->ProjectPointToNavigation(DestinationHit.Location, NavLocation, TeleportProjectionExtent);
  81.  
  82.     OutLocation = NavLocation.Location;
  83.  
  84.     return bValidNavProjection && bValidHit;
  85. }
  86.  
  87. void AVRCharacter::UpdateDestinatonMarker()
  88. {
  89.     FVector DestinationLocation;
  90.     bool bHasDestination = FindTeleportDestination(DestinationLocation);
  91.  
  92.     if (bHasDestination)
  93.     {
  94.         DestinationMarker->SetWorldLocation(DestinationLocation);
  95.         if (!DestinationMarker->IsVisible())
  96.         {
  97.             DestinationMarker->SetVisibility(true);
  98.         }
  99.         return;
  100.     }
  101.     DestinationMarker->SetVisibility(false);
  102. }
  103.  
  104. void AVRCharacter::MoveForward(float Throttle)
  105. {
  106.     AddMovementInput(VRCamera->GetForwardVector() * Throttle);
  107. }
  108.  
  109. void AVRCharacter::MoveRight(float Throttle)
  110. {
  111.     AddMovementInput(VRCamera->GetRightVector() * Throttle);
  112.  
  113. }
  114.  
  115. void AVRCharacter::BeginTeleport()
  116. {
  117.     StartFade(0,1);
  118.     FTimerHandle TimerHandle;
  119.     GetWorldTimerManager().SetTimer(TimerHandle, this, &AVRCharacter::FinishTeleport, TeleportFadeTime);
  120.  
  121.  
  122. }
  123.  
  124. void AVRCharacter::FinishTeleport()
  125. {
  126.    
  127.     FVector TeleportDestination = DestinationMarker->GetComponentLocation();
  128.     TeleportDestination.Z = TeleportDestination.Z + GetCapsuleComponent()->GetScaledCapsuleHalfHeight();
  129.     SetActorLocation(TeleportDestination);
  130.    
  131.     StartFade(1, 0);
  132. }
  133.  
  134. void AVRCharacter::StartFade(float FromAlpha, float ToAlpha)
  135. {
  136.     APlayerController* PlayerController = Cast<APlayerController>(GetController());
  137.     if (PlayerController != nullptr)
  138.     {
  139.         PlayerController->PlayerCameraManager->StartCameraFade(FromAlpha, ToAlpha, TeleportFadeTime, FLinearColor::Black);
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement