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 "VRCharacter.h"
- #include "Camera/CameraComponent.h"
- #include "Components/InputComponent.h"
- #include "Components/StaticMeshComponent.h"
- #include "GameFramework/PlayerController.h"
- #include "TimerManager.h"
- #include "Components/CapsuleComponent.h"
- #include "NavigationSystem.h"
- // Sets default values
- AVRCharacter::AVRCharacter()
- {
- // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- VRFeet = CreateDefaultSubobject<USceneComponent>(TEXT("VRFeet"));
- VRFeet->SetupAttachment(GetRootComponent());
- VRFeet->SetRelativeLocation(FVector(0, 0, -88));
- VRCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("VRCamera"));
- VRCamera->SetupAttachment(VRFeet);
- DestinationMarker = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("DestinatioNMarker"));
- DestinationMarker->SetupAttachment(GetRootComponent());
- //CharacterMovement->MaxWalkSpeed = 300.0f;
- }
- // Called when the game starts or when spawned
- void AVRCharacter::BeginPlay()
- {
- Super::BeginPlay();
- DestinationMarker->SetVisibility(false);
- DestinationMarker->SetCollisionEnabled(ECollisionEnabled::NoCollision);
- }
- // Called every frame
- void AVRCharacter::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- //Move VRCamera back to root so that VRCamera aligns with Character Capsule Collision.
- FVector NewCameraOffset = VRCamera->GetComponentLocation() - GetActorLocation();
- NewCameraOffset.Z = 0.0f;
- AddActorWorldOffset(NewCameraOffset);
- VRFeet->AddWorldOffset(-NewCameraOffset);
- UpdateDestinatonMarker();
- }
- // Called to bind functionality to input
- void AVRCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AVRCharacter::MoveForward);
- PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AVRCharacter::MoveRight);
- PlayerInputComponent->BindAction(TEXT("Teleport"), IE_Released, this, &AVRCharacter::BeginTeleport);
- }
- bool AVRCharacter::FindTeleportDestination(FVector &OutLocation)
- {
- FVector StartHit = VRCamera->GetComponentLocation();
- FVector EndHit = StartHit + VRCamera->GetForwardVector() * MaxTeleportDistance;
- FHitResult DestinationHit;
- UNavigationSystemV1* NavSystem = UNavigationSystemV1::GetCurrent(GetWorld());
- if (!NavSystem) { UE_LOG(LogTemp, Warning, TEXT("No NavSystem")); return false; }
- FNavLocation NavLocation;
- bool bValidHit = GetWorld()->LineTraceSingleByChannel(DestinationHit, StartHit, EndHit, ECC_Visibility);
- bool bValidNavProjection = NavSystem->ProjectPointToNavigation(DestinationHit.Location, NavLocation, TeleportProjectionExtent);
- OutLocation = NavLocation.Location;
- return bValidNavProjection && bValidHit;
- }
- void AVRCharacter::UpdateDestinatonMarker()
- {
- FVector DestinationLocation;
- bool bHasDestination = FindTeleportDestination(DestinationLocation);
- if (bHasDestination)
- {
- DestinationMarker->SetWorldLocation(DestinationLocation);
- if (!DestinationMarker->IsVisible())
- {
- DestinationMarker->SetVisibility(true);
- }
- return;
- }
- DestinationMarker->SetVisibility(false);
- }
- void AVRCharacter::MoveForward(float Throttle)
- {
- AddMovementInput(VRCamera->GetForwardVector() * Throttle);
- }
- void AVRCharacter::MoveRight(float Throttle)
- {
- AddMovementInput(VRCamera->GetRightVector() * Throttle);
- }
- void AVRCharacter::BeginTeleport()
- {
- StartFade(0,1);
- FTimerHandle TimerHandle;
- GetWorldTimerManager().SetTimer(TimerHandle, this, &AVRCharacter::FinishTeleport, TeleportFadeTime);
- }
- void AVRCharacter::FinishTeleport()
- {
- FVector TeleportDestination = DestinationMarker->GetComponentLocation();
- TeleportDestination.Z = TeleportDestination.Z + GetCapsuleComponent()->GetScaledCapsuleHalfHeight();
- SetActorLocation(TeleportDestination);
- StartFade(1, 0);
- }
- void AVRCharacter::StartFade(float FromAlpha, float ToAlpha)
- {
- APlayerController* PlayerController = Cast<APlayerController>(GetController());
- if (PlayerController != nullptr)
- {
- PlayerController->PlayerCameraManager->StartCameraFade(FromAlpha, ToAlpha, TeleportFadeTime, FLinearColor::Black);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement