A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
PlayerAlignComponent.cpp
Go to the documentation of this file.
1// Copyright(c) Pixo Group. All Rights Reserved.
2
4#include "PixoVRCharacter.h"
5#include "Kismet/GameplayStatics.h"
6#include "Kismet/KismetMathLibrary.h"
7
8// TODO Better we allow not only to track a character, allow every actor that might be interresting.
9
11 : PixoVRCharacter(nullptr)
12 , RotationOffset(0.0f, 0.0f, 0.0f)
13 , bLockRoll(false)
14 , bLockPitch(false)
15 , bLockYaw(false)
16 , AlignmentSpeed(8.0f)
17{
18 PrimaryComponentTick.bCanEverTick = true;
19}
20
22{
23 Super::BeginPlay();
24}
25
26void UPlayerAlignComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
27{
28 Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
29
30 // Did the user provide a character we should align to?
32 if(!Character)
33 {
34 // No, get the first player and use ID = 0.
35 if(APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
36 {
37 Character = Cast<APixoVRCharacter>(PlayerController->GetPawn());
38 }
39 }
40
41 if(Character)
42 {
43 FRotator Rotator = UKismetMathLibrary::FindLookAtRotation(GetComponentLocation(), Character->VRReplicatedCamera->GetComponentLocation());
44 Rotator.Add(RotationOffset.Pitch, RotationOffset.Yaw, RotationOffset.Roll);
45 if(bLockYaw)
46 {
47 Rotator.Yaw = RotationOffset.Yaw;
48 }
49
50 if(bLockRoll)
51 {
52 Rotator.Roll = RotationOffset.Roll;
53 }
54
55 if(bLockPitch)
56 {
57 Rotator.Pitch = RotationOffset.Pitch;
58 }
59
60 if(AlignmentSpeed > 0.0f)
61 {
62 Rotator = UKismetMathLibrary::RInterpTo(GetComponentRotation(),
63 Rotator,
64 DeltaTime,
66 }
67
68 SetWorldRotation(Rotator);
69 }
70}
71
72#if WITH_EDITOR
73void UPlayerAlignComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
74{
75 if(PropertyChangedEvent.Property != nullptr)
76 {
77 const FName PropertyName (PropertyChangedEvent.Property->GetFName());
78 if(PropertyName == GET_MEMBER_NAME_CHECKED(UPlayerAlignComponent, RotationOffset))
79 {
80 SetRelativeRotation(RotationOffset);
81 }
82 }
83
84 Super::PostEditChangeProperty(PropertyChangedEvent);
85}
86#endif
87
88void UPlayerAlignComponent::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
89{
90 Super::GetLifetimeReplicatedProps(OutLifetimeProps);
91
93}
Pixo VR Character This class represents the main character in the Pixo VR game. It extends the AVRCha...
UReplicatedVRCameraComponent * VRReplicatedCamera
UPROPERTY(Category = VRBaseCharacter, VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess ...
Component that aligns an object to player.
FRotator RotationOffset
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PixoVR | Align")
virtual void BeginPlay() override
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override
bool bLockPitch
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PixoVR | Align")
bool bLockYaw
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PixoVR | Align")
float AlignmentSpeed
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PixoVR | Align")
APixoVRCharacter * PixoVRCharacter
UPROPERTY(BlueprintReadWrite, Replicated, Category = "PixovR | Align")
bool bLockRoll
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PixoVR | Align")