├── .gitignore ├── ActorLineTrace ├── ActorLineTrace.cpp └── ActorLineTrace.h ├── AddBillboardComp ├── AddBillboardComp.cpp └── AddBillboardComp.h ├── AddForceOnFire ├── UnrealCPPCharacter.cpp.txt └── UnrealCPPCharacter.h.txt ├── AddMeshFromFile ├── AddMeshFromFile.cpp └── AddMeshFromFile.h ├── AddRadialForce ├── AddRadialForce.cpp └── AddRadialForce.h ├── CameraDirector ├── CameraDirector.cpp └── CameraDirector.h ├── CameraShake ├── MyCameraShake.cpp ├── MyCameraShake.h ├── UnrealCPPCharacter.cpp.txt └── UnrealCPPCharacter.h.txt ├── ChangeMaterialMesh ├── ChangeMaterialMesh.cpp └── ChangeMaterialMesh.h ├── CharacterBindButton ├── UnrealCPPCharacter.cpp.txt └── UnrealCPPCharacter.h.txt ├── CharacterOverlapEvents ├── UnrealCPPCharacter.cpp.txt └── UnrealCPPCharacter.h.txt ├── CollidingPawn ├── CollidingPawn.cpp ├── CollidingPawn.h ├── CollidingPawnMovementComponent.cpp └── CollidingPawnMovementComponent.h ├── ConsoleLog ├── ConsoleLog.cpp └── ConsoleLog.h ├── CreateStaticMesh ├── CreateStaticMesh.cpp └── CreateStaticMesh.h ├── DefaultStarterFiles ├── UnrealCPP.Build.cs.txt ├── UnrealCPP.cpp.txt ├── UnrealCPP.h.txt ├── UnrealCPPCharacter.cpp.txt ├── UnrealCPPCharacter.h.txt ├── UnrealCPPGameMode.cpp.txt ├── UnrealCPPGameMode.h.txt ├── UnrealCPPHUD.cpp.txt ├── UnrealCPPHUD.h.txt ├── UnrealCPPProjectile.cpp.txt └── UnrealCPPProjectile.h.txt ├── DestroyActorOnOverlap ├── DestroyActorOnOverlap.cpp └── DestroyActorOnOverlap.h ├── FindPlayerPosition ├── BP_FindPlayerPosition.png ├── FindPlayerPosition.cpp └── FindPlayerPosition.h ├── FloatingActor ├── FloatingActor.cpp └── FloatingActor.h ├── GetNumberOfPawns ├── GetNumberOfPawns.cpp └── GetNumberOfPawns.h ├── HealthBar ├── CampFire.cpp.txt ├── CampFire.h.txt ├── MedKit.cpp.txt ├── MedKit.h.txt ├── UnrealCPP.Build.cs.txt ├── UnrealCPPCharacter.cpp.txt ├── UnrealCPPCharacter.h.txt ├── UnrealCPPGameMode.cpp.txt └── UnrealCPPGameMode.h.txt ├── HideActor ├── HideActor.cpp └── HideActor.h ├── InventoryArray ├── InventoryItem.cpp └── InventoryItem.h ├── LICENSE ├── LightSwitchPushButton ├── LightSwitchPushButton.cpp ├── LightSwitchPushButton.h ├── UnrealCPPCharacter.cpp.txt └── UnrealCPPCharacter.h.txt ├── LightSwitchTrigger ├── LightSwitchTrigger.cpp └── LightSwitchTrigger.h ├── LineTraceOnFire └── UnrealCPPCharacter.cpp.txt ├── LineTraceOnTick ├── UnrealCPPCharacter.cpp.txt └── UnrealCPPCharacter.h.txt ├── MyDrawDebugHelpers ├── MyDrawDebugHelpers.cpp └── MyDrawDebugHelpers.h ├── MyInterface ├── MyInterface.cpp ├── MyInterface.h ├── MyInterfaceActor.cpp ├── MyInterfaceActor.h ├── MyInterfaceActorTwo.cpp ├── MyInterfaceActorTwo.h ├── MyInterfaceTriggerVolume.cpp └── MyInterfaceTriggerVolume.h ├── MyPawn ├── MyPawn.cpp └── MyPawn.h ├── MyStaticMeshActor ├── MyStaticMeshActor.cpp └── MyStaticMeshActor.h ├── MySweepActor ├── MySweepActor.cpp └── MySweepActor.h ├── OnComponentHit ├── OnComponentHit.cpp └── OnComponentHit.h ├── OpenDoorTimelineCurve ├── OpenDoorTimelineCurve.cpp ├── OpenDoorTimelineCurve.h ├── UnrealCPPCharacter.cpp.txt ├── UnrealCPPCharacter.h.txt └── UnrealCpp.Build.cs.txt ├── OpenDoorTimelineOverlap ├── OpenDoorTimelineOverlap.cpp └── OpenDoorTimelineOverlap.h ├── OpenDoorWithLerp ├── OpenDoorWithLerp.cpp └── OpenDoorWithLerp.h ├── PickupAndRotateActor ├── PickupAndRotateActor.cpp ├── PickupAndRotateActor.h ├── UnrealCPPCharacter.cpp.txt └── UnrealCPPCharacter.h.txt ├── README.md ├── RotateActorAroundPlayer ├── RotateActorAroundPlayer.cpp └── RotateActorAroundPlayer.h ├── RotateActorAroundWithRotation ├── RotateActorAroundWithRotation.cpp └── RotateActorAroundWithRotation.h ├── RotateAngleAxis ├── RotatingAngleAxis.cpp └── RotatingAngleAxis.h ├── RotateAroundVector ├── RotateAroundVector.cpp └── RotateAroundVector.h ├── RotatingActor ├── RotatingActor.cpp └── RotatingActor.h ├── SetActorLocationAndRotation ├── SetActorLocationAndRotation.cpp └── SetActorLocationAndRotation.h ├── SetViewTarget ├── SetViewTarget.cpp └── SetViewTarget.h ├── SetViewTargetBlend ├── SetViewTargetBlend.cpp └── SetViewTargetBlend.h ├── ShooterProjectile ├── ShooterProjectile.cpp └── ShooterProjectile.h ├── SwingDoor ├── SwingDoor.cpp ├── SwingDoor.h ├── UCPPCharacter.cpp └── UCPPCharacter.h ├── TimerActor ├── TimerActor.cpp └── TimerActor.h ├── TriggerBox ├── MyTriggerBox.cpp └── MyTriggerBox.h ├── TriggerBoxSpecificActor ├── TriggerBoxSpecificActor.cpp └── TriggerBoxSpecificActor.h ├── TriggerVolume ├── MyTriggerVolume.cpp └── MyTriggerVolume.h ├── TriggerVolumeSpecificActor ├── TriggerVolumeSpecificActor.cpp └── TriggerVolumeSpecificActor.h ├── UnrealCPP.Build.cs ├── UnrealCPP.cpp ├── UnrealCPP.h ├── UnrealCPPCharacter.cpp ├── UnrealCPPCharacter.h ├── UnrealCPPGameMode.cpp ├── UnrealCPPGameMode.h ├── UnrealCPPHUD.cpp ├── UnrealCPPHUD.h ├── UnrealCPPProjectile.cpp └── UnrealCPPProjectile.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | notes.txt -------------------------------------------------------------------------------- /ActorLineTrace/ActorLineTrace.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "ActorLineTrace.h" 8 | #include "ConstructorHelpers.h" 9 | #include "DrawDebugHelpers.h" 10 | 11 | // Sets default values 12 | AActorLineTrace::AActorLineTrace() 13 | { 14 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 15 | PrimaryActorTick.bCanEverTick = true; 16 | 17 | // add cube to root 18 | Cube = CreateDefaultSubobject(TEXT("Visual Representation")); 19 | RootComponent = Cube; 20 | 21 | static ConstructorHelpers::FObjectFinder CubeAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube")); 22 | 23 | if (CubeAsset.Succeeded()) 24 | { 25 | Cube->SetStaticMesh(CubeAsset.Object); 26 | Cube->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f)); 27 | Cube->SetWorldScale3D(FVector(1.f)); 28 | } 29 | 30 | // add another component in the editor to the actor to overlap with the line trace to get the event to fire 31 | 32 | } 33 | 34 | // Called every frame 35 | void AActorLineTrace::Tick(float DeltaTime) 36 | { 37 | Super::Tick(DeltaTime); 38 | 39 | FHitResult OutHit; 40 | FVector Start = GetActorLocation(); 41 | 42 | // Start.Z += 50.f; 43 | // Start.X += 200.f; 44 | 45 | FVector ForwardVector = GetActorForwardVector(); 46 | FVector End = ((ForwardVector * 500.f) + Start); 47 | FCollisionQueryParams CollisionParams; 48 | CollisionParams.bTraceComplex = true; 49 | CollisionParams.AddIgnoredComponent(Cube); 50 | // or you could use either of the below to ignore the root cube 51 | // CollisionParams.AddIgnoredComponent_LikelyDuplicatedRoot(Cube); 52 | // CollisionParams.AddIgnoredActor(this); 53 | 54 | DrawDebugLine(GetWorld(), Start, End, FColor::Green, false, 1, 0, 5); 55 | 56 | if(ActorLineTraceSingle(OutHit, Start, End, ECC_WorldStatic, CollisionParams)) 57 | { 58 | GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Green, FString::Printf(TEXT("The Component Being Hit is: %s"), *OutHit.GetComponent()->GetName())); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /ActorLineTrace/ActorLineTrace.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "ActorLineTrace.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AActorLineTrace : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | AActorLineTrace(); 20 | 21 | public: 22 | // Called every frame 23 | virtual void Tick(float DeltaTime) override; 24 | 25 | UPROPERTY(EditAnywhere) 26 | UStaticMeshComponent* Cube; 27 | }; 28 | -------------------------------------------------------------------------------- /AddBillboardComp/AddBillboardComp.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "AddBillboardComp.h" 8 | // include billboard comp 9 | #include "Components/BillboardComponent.h" 10 | 11 | // Sets default values 12 | AAddBillboardComp::AAddBillboardComp() 13 | { 14 | MyBillboardComp = CreateDefaultSubobject(TEXT("Root Billboard Comp")); 15 | MyBillboardComp->SetHiddenInGame(false, true); 16 | RootComponent = MyBillboardComp; 17 | } 18 | -------------------------------------------------------------------------------- /AddBillboardComp/AddBillboardComp.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "AddBillboardComp.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AAddBillboardComp : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | AAddBillboardComp(); 20 | 21 | // declare point billboard comp 22 | UPROPERTY(VisibleAnywhere) 23 | class UBillboardComponent* MyBillboardComp; 24 | 25 | }; 26 | -------------------------------------------------------------------------------- /AddForceOnFire/UnrealCPPCharacter.h.txt: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "CoreMinimal.h" 10 | #include "GameFramework/Character.h" 11 | #include "UnrealCPPCharacter.generated.h" 12 | 13 | class UInputComponent; 14 | 15 | UCLASS(config=Game) 16 | class AUnrealCPPCharacter : public ACharacter 17 | { 18 | GENERATED_BODY() 19 | 20 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 21 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 22 | class USkeletalMeshComponent* Mesh1P; 23 | 24 | /** Gun mesh: 1st person view (seen only by self) */ 25 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 26 | class USkeletalMeshComponent* FP_Gun; 27 | 28 | /** Location on gun mesh where projectiles should spawn. */ 29 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 30 | class USceneComponent* FP_MuzzleLocation; 31 | 32 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 33 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 34 | class USkeletalMeshComponent* VR_Gun; 35 | 36 | /** Location on VR gun mesh where projectiles should spawn. */ 37 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 38 | class USceneComponent* VR_MuzzleLocation; 39 | 40 | /** First person camera */ 41 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 42 | class UCameraComponent* FirstPersonCameraComponent; 43 | 44 | /** Motion controller (right hand) */ 45 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 46 | class UMotionControllerComponent* R_MotionController; 47 | 48 | /** Motion controller (left hand) */ 49 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 50 | class UMotionControllerComponent* L_MotionController; 51 | 52 | public: 53 | AUnrealCPPCharacter(); 54 | 55 | protected: 56 | virtual void BeginPlay(); 57 | 58 | public: 59 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 60 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 61 | float BaseTurnRate; 62 | 63 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 64 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 65 | float BaseLookUpRate; 66 | 67 | /** Gun muzzle's offset from the characters location */ 68 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 69 | FVector GunOffset; 70 | 71 | /** Projectile class to spawn */ 72 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 73 | TSubclassOf ProjectileClass; 74 | 75 | /** Sound to play each time we fire */ 76 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 77 | class USoundBase* FireSound; 78 | 79 | /** AnimMontage to play each time we fire */ 80 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 81 | class UAnimMontage* FireAnimation; 82 | 83 | /** Whether to use motion controller location for aiming. */ 84 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 85 | uint32 bUsingMotionControllers : 1; 86 | 87 | protected: 88 | 89 | /** Fires a projectile. */ 90 | void OnFire(); 91 | 92 | /** Resets HMD orientation and position in VR. */ 93 | void OnResetVR(); 94 | 95 | /** Handles moving forward/backward */ 96 | void MoveForward(float Val); 97 | 98 | /** Handles stafing movement, left and right */ 99 | void MoveRight(float Val); 100 | 101 | /** 102 | * Called via input to turn at a given rate. 103 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 104 | */ 105 | void TurnAtRate(float Rate); 106 | 107 | /** 108 | * Called via input to turn look up/down at a given rate. 109 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 110 | */ 111 | void LookUpAtRate(float Rate); 112 | 113 | struct TouchData 114 | { 115 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 116 | bool bIsPressed; 117 | ETouchIndex::Type FingerIndex; 118 | FVector Location; 119 | bool bMoved; 120 | }; 121 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 122 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 123 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 124 | TouchData TouchItem; 125 | 126 | protected: 127 | // APawn interface 128 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 129 | // End of APawn interface 130 | 131 | /* 132 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 133 | * 134 | * @param InputComponent The input component pointer to bind controls to 135 | * @returns true if touch controls were enabled. 136 | */ 137 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 138 | 139 | public: 140 | /** Returns Mesh1P subobject **/ 141 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 142 | /** Returns FirstPersonCameraComponent subobject **/ 143 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 144 | 145 | }; 146 | -------------------------------------------------------------------------------- /AddMeshFromFile/AddMeshFromFile.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "AddMeshFromFile.h" 8 | // add constructor header 9 | #include "UObject/ConstructorHelpers.h" 10 | 11 | // Sets default values 12 | AAddMeshFromFile::AAddMeshFromFile() 13 | { 14 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 15 | PrimaryActorTick.bCanEverTick = true; 16 | 17 | // add Cylinder to root 18 | UStaticMeshComponent* Cylinder = CreateDefaultSubobject(TEXT("VisualRepresentation")); 19 | RootComponent = Cylinder; 20 | 21 | static ConstructorHelpers::FObjectFinder CylinderAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cylinder.Shape_Cylinder")); 22 | 23 | if (CylinderAsset.Succeeded()) 24 | { 25 | Cylinder->SetStaticMesh(CylinderAsset.Object); 26 | Cylinder->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f)); 27 | Cylinder->SetWorldScale3D(FVector(1.f)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /AddMeshFromFile/AddMeshFromFile.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "AddMeshFromFile.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AAddMeshFromFile : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | AAddMeshFromFile(); 20 | }; 21 | -------------------------------------------------------------------------------- /AddRadialForce/AddRadialForce.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "AddRadialForce.h" 8 | // add debug helpfers 9 | #include "DrawDebugHelpers.h" 10 | 11 | // Called when the game starts or when spawned 12 | void AAddRadialForce::BeginPlay() 13 | { 14 | Super::BeginPlay(); 15 | 16 | // create tarray for hit results 17 | TArray OutHits; 18 | 19 | // get actor locations 20 | FVector MyLocation = GetActorLocation(); 21 | 22 | // create a collision sphere 23 | FCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f); 24 | 25 | // draw collision sphere 26 | DrawDebugSphere(GetWorld(), GetActorLocation(), MyColSphere.GetSphereRadius(), 50, FColor::Cyan, true); 27 | 28 | // check if something got hit in the sweep 29 | bool isHit = GetWorld()->SweepMultiByChannel(OutHits, MyLocation, MyLocation, FQuat::Identity, ECC_WorldStatic, MyColSphere); 30 | 31 | if (isHit) 32 | { 33 | // loop through TArray 34 | for (auto& Hit : OutHits) 35 | { 36 | UStaticMeshComponent* MeshComp = Cast((Hit.GetActor())->GetRootComponent()); 37 | 38 | if (MeshComp) 39 | { 40 | // alternivly you can use ERadialImpulseFalloff::RIF_Linear for the impulse to get linearly weaker as it gets further from origin. 41 | // set the float radius to 500 and the float strength to 2000. 42 | MeshComp->AddRadialImpulse(GetActorLocation(), 500.f, 2000.f, ERadialImpulseFalloff::RIF_Constant, true); 43 | } 44 | } 45 | } 46 | 47 | Destroy(); 48 | 49 | } 50 | -------------------------------------------------------------------------------- /AddRadialForce/AddRadialForce.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | 10 | #include "GameFramework/Actor.h" 11 | #include "AddRadialForce.generated.h" 12 | 13 | UCLASS() 14 | class UNREALCPP_API AAddRadialForce : public AActor 15 | { 16 | GENERATED_BODY() 17 | 18 | protected: 19 | // Called when the game starts or when spawned 20 | virtual void BeginPlay() override; 21 | }; 22 | -------------------------------------------------------------------------------- /CameraDirector/CameraDirector.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "CameraDirector.h" 8 | // include gameplay statics 9 | #include "Kismet/GameplayStatics.h" 10 | 11 | // Sets default values 12 | ACameraDirector::ACameraDirector() 13 | { 14 | PrimaryActorTick.bCanEverTick = true; 15 | } 16 | 17 | // Called every frame 18 | void ACameraDirector::Tick(float DeltaTime) 19 | { 20 | Super::Tick(DeltaTime); 21 | 22 | const float TimeBetweenCameraChanges = 2.0f; 23 | const float SmoothBlendTime = 0.75f; 24 | TimeToNextCameraChange -= DeltaTime; 25 | 26 | if (TimeToNextCameraChange <= 0.0f) 27 | { 28 | TimeToNextCameraChange += TimeBetweenCameraChanges; 29 | 30 | //Find the actor that handles control for the local player. 31 | APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0); 32 | if (OurPlayerController) 33 | { 34 | if (CameraTwo && (OurPlayerController->GetViewTarget() == CameraOne)) 35 | { 36 | //Blend smoothly to camera two. 37 | OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime); 38 | } 39 | else if (CameraOne) 40 | { 41 | //Cut instantly to camera one. 42 | OurPlayerController->SetViewTarget(CameraOne); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /CameraDirector/CameraDirector.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "CameraDirector.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ACameraDirector : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | ACameraDirector(); 19 | 20 | // Called every frame 21 | virtual void Tick(float DeltaTime) override; 22 | 23 | // declare variables 24 | UPROPERTY(EditAnywhere) 25 | AActor* CameraOne; 26 | 27 | UPROPERTY(EditAnywhere) 28 | AActor* CameraTwo; 29 | 30 | float TimeToNextCameraChange; 31 | 32 | }; 33 | -------------------------------------------------------------------------------- /CameraShake/MyCameraShake.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MyCameraShake.h" 8 | 9 | // Helpful Links 10 | // http://api.unrealengine.com/INT/API/Runtime/Engine/Camera/UCameraShake/index.html 11 | // 12 | // Great explanation of camera shake values 13 | // https://www.youtube.com/watch?v=Oice8gdpX6s 14 | 15 | #include "MyCameraShake.h" 16 | 17 | // Sets default values 18 | UMyCameraShake::UMyCameraShake() 19 | { 20 | OscillationDuration = 0.25f; 21 | OscillationBlendInTime = 0.05f; 22 | OscillationBlendOutTime = 0.05f; 23 | 24 | RotOscillation.Pitch.Amplitude = FMath::RandRange(5.0f, 10.0f); 25 | RotOscillation.Pitch.Frequency = FMath::RandRange(25.0f, 35.0f); 26 | 27 | RotOscillation.Yaw.Amplitude = FMath::RandRange(5.0f, 10.0f); 28 | RotOscillation.Yaw.Frequency = FMath::RandRange(25.0f, 35.0f); 29 | } 30 | -------------------------------------------------------------------------------- /CameraShake/MyCameraShake.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "Camera/CameraShake.h" 10 | #include "MyCameraShake.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API UMyCameraShake : public UCameraShake 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | UMyCameraShake(); 20 | 21 | }; 22 | -------------------------------------------------------------------------------- /CameraShake/UnrealCPPCharacter.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Character.h" 7 | #include "UnrealCPPCharacter.generated.h" 8 | 9 | class UInputComponent; 10 | 11 | UCLASS(config=Game) 12 | class AUnrealCPPCharacter : public ACharacter 13 | { 14 | GENERATED_BODY() 15 | 16 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 17 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 18 | class USkeletalMeshComponent* Mesh1P; 19 | 20 | /** Gun mesh: 1st person view (seen only by self) */ 21 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 22 | class USkeletalMeshComponent* FP_Gun; 23 | 24 | /** Location on gun mesh where projectiles should spawn. */ 25 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 26 | class USceneComponent* FP_MuzzleLocation; 27 | 28 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 29 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 30 | class USkeletalMeshComponent* VR_Gun; 31 | 32 | /** Location on VR gun mesh where projectiles should spawn. */ 33 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 34 | class USceneComponent* VR_MuzzleLocation; 35 | 36 | /** First person camera */ 37 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 38 | class UCameraComponent* FirstPersonCameraComponent; 39 | 40 | /** Motion controller (right hand) */ 41 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 42 | class UMotionControllerComponent* R_MotionController; 43 | 44 | /** Motion controller (left hand) */ 45 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 46 | class UMotionControllerComponent* L_MotionController; 47 | 48 | public: 49 | AUnrealCPPCharacter(); 50 | 51 | protected: 52 | virtual void BeginPlay(); 53 | 54 | public: 55 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 56 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 57 | float BaseTurnRate; 58 | 59 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 60 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 61 | float BaseLookUpRate; 62 | 63 | /** Gun muzzle's offset from the characters location */ 64 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 65 | FVector GunOffset; 66 | 67 | /** Projectile class to spawn */ 68 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 69 | TSubclassOf ProjectileClass; 70 | 71 | /** Sound to play each time we fire */ 72 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 73 | class USoundBase* FireSound; 74 | 75 | /** AnimMontage to play each time we fire */ 76 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 77 | class UAnimMontage* FireAnimation; 78 | 79 | /** Whether to use motion controller location for aiming. */ 80 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 81 | uint32 bUsingMotionControllers : 1; 82 | 83 | UPROPERTY(EditAnywhere) 84 | TSubclassOf MyShake; 85 | 86 | protected: 87 | 88 | /** Fires a projectile. */ 89 | void OnFire(); 90 | 91 | /** Resets HMD orientation and position in VR. */ 92 | void OnResetVR(); 93 | 94 | /** Handles moving forward/backward */ 95 | void MoveForward(float Val); 96 | 97 | /** Handles stafing movement, left and right */ 98 | void MoveRight(float Val); 99 | 100 | /** 101 | * Called via input to turn at a given rate. 102 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 103 | */ 104 | void TurnAtRate(float Rate); 105 | 106 | /** 107 | * Called via input to turn look up/down at a given rate. 108 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 109 | */ 110 | void LookUpAtRate(float Rate); 111 | 112 | struct TouchData 113 | { 114 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 115 | bool bIsPressed; 116 | ETouchIndex::Type FingerIndex; 117 | FVector Location; 118 | bool bMoved; 119 | }; 120 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 121 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 122 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 123 | TouchData TouchItem; 124 | 125 | protected: 126 | // APawn interface 127 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 128 | // End of APawn interface 129 | 130 | /* 131 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 132 | * 133 | * @param InputComponent The input component pointer to bind controls to 134 | * @returns true if touch controls were enabled. 135 | */ 136 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 137 | 138 | public: 139 | /** Returns Mesh1P subobject **/ 140 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 141 | /** Returns FirstPersonCameraComponent subobject **/ 142 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 143 | 144 | }; 145 | -------------------------------------------------------------------------------- /ChangeMaterialMesh/ChangeMaterialMesh.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | //helpful links 8 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UMeshComponent/SetMaterial/index.html 9 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/OnComponentBeginOverlap/ 10 | 11 | #include "ChangeMaterialMesh.h" 12 | // include draw debug helpers header file and the box component 13 | #include "DrawDebugHelpers.h" 14 | #include "Components/BoxComponent.h" 15 | 16 | // Sets default values 17 | AChangeMaterialMesh::AChangeMaterialMesh() 18 | { 19 | 20 | MyMesh = CreateDefaultSubobject(TEXT("My Mesh")); 21 | MyMesh->SetupAttachment(RootComponent); 22 | RootComponent = MyMesh; 23 | 24 | MyBoxComponent = CreateDefaultSubobject(TEXT("My BoxComponent")); 25 | MyBoxComponent->InitBoxExtent(FVector(100,100,100)); 26 | MyBoxComponent->SetCollisionProfileName("Trigger"); 27 | MyBoxComponent->SetupAttachment(RootComponent); 28 | 29 | OnMaterial = CreateDefaultSubobject(TEXT("OnMaterial")); 30 | OffMaterial = CreateDefaultSubobject(TEXT("OffMaterial")); 31 | 32 | MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AChangeMaterialMesh::OnOverlapBegin); 33 | 34 | } 35 | 36 | // Called when the game starts or when spawned 37 | void AChangeMaterialMesh::BeginPlay() 38 | { 39 | Super::BeginPlay(); 40 | 41 | DrawDebugBox(GetWorld(), GetActorLocation(), FVector(100,100,100), FColor::White, true, -1, 0, 10); 42 | 43 | MyMesh->SetMaterial(0, OffMaterial); 44 | } 45 | 46 | void AChangeMaterialMesh::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) 47 | { 48 | if ( (OtherActor != nullptr ) && (OtherActor != this) && ( OtherComp != nullptr ) ) 49 | { 50 | MyMesh->SetMaterial(0, OnMaterial); 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /ChangeMaterialMesh/ChangeMaterialMesh.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "ChangeMaterialMesh.generated.h" 11 | 12 | class UBoxComponent; 13 | 14 | UCLASS() 15 | class UNREALCPP_API AChangeMaterialMesh : public AActor 16 | { 17 | GENERATED_BODY() 18 | 19 | public: 20 | // Sets default values for this actor's properties 21 | AChangeMaterialMesh(); 22 | 23 | protected: 24 | // Called when the game starts or when spawned 25 | virtual void BeginPlay() override; 26 | 27 | public: 28 | 29 | UPROPERTY(VisibleAnywhere) 30 | class UStaticMeshComponent* MyMesh; 31 | 32 | UPROPERTY(EditAnywhere) 33 | class UMaterialInterface* OnMaterial; 34 | 35 | UPROPERTY(EditAnywhere) 36 | class UMaterialInterface* OffMaterial; 37 | 38 | UPROPERTY() 39 | class UBoxComponent* MyBoxComponent; 40 | 41 | UFUNCTION() 42 | void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); 43 | 44 | }; 45 | -------------------------------------------------------------------------------- /CharacterBindButton/UnrealCPPCharacter.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Character.h" 7 | #include "UnrealCPPCharacter.generated.h" 8 | 9 | class UInputComponent; 10 | 11 | UCLASS(config=Game) 12 | class AUnrealCPPCharacter : public ACharacter 13 | { 14 | GENERATED_BODY() 15 | 16 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 17 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 18 | class USkeletalMeshComponent* Mesh1P; 19 | 20 | /** Gun mesh: 1st person view (seen only by self) */ 21 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 22 | class USkeletalMeshComponent* FP_Gun; 23 | 24 | /** Location on gun mesh where projectiles should spawn. */ 25 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 26 | class USceneComponent* FP_MuzzleLocation; 27 | 28 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 29 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 30 | class USkeletalMeshComponent* VR_Gun; 31 | 32 | /** Location on VR gun mesh where projectiles should spawn. */ 33 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 34 | class USceneComponent* VR_MuzzleLocation; 35 | 36 | /** First person camera */ 37 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 38 | class UCameraComponent* FirstPersonCameraComponent; 39 | 40 | /** Motion controller (right hand) */ 41 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 42 | class UMotionControllerComponent* R_MotionController; 43 | 44 | /** Motion controller (left hand) */ 45 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 46 | class UMotionControllerComponent* L_MotionController; 47 | 48 | public: 49 | AUnrealCPPCharacter(); 50 | 51 | protected: 52 | virtual void BeginPlay(); 53 | 54 | public: 55 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 56 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 57 | float BaseTurnRate; 58 | 59 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 60 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 61 | float BaseLookUpRate; 62 | 63 | /** Gun muzzle's offset from the characters location */ 64 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 65 | FVector GunOffset; 66 | 67 | /** Projectile class to spawn */ 68 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 69 | TSubclassOf ProjectileClass; 70 | 71 | /** Sound to play each time we fire */ 72 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 73 | class USoundBase* FireSound; 74 | 75 | /** AnimMontage to play each time we fire */ 76 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 77 | class UAnimMontage* FireAnimation; 78 | 79 | /** Whether to use motion controller location for aiming. */ 80 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 81 | uint32 bUsingMotionControllers : 1; 82 | 83 | protected: 84 | 85 | /** Fires a projectile. */ 86 | void OnFire(); 87 | 88 | /** adding onAction function. */ 89 | void OnAction(); 90 | 91 | /** Resets HMD orientation and position in VR. */ 92 | void OnResetVR(); 93 | 94 | /** Handles moving forward/backward */ 95 | void MoveForward(float Val); 96 | 97 | /** Handles stafing movement, left and right */ 98 | void MoveRight(float Val); 99 | 100 | /** 101 | * Called via input to turn at a given rate. 102 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 103 | */ 104 | void TurnAtRate(float Rate); 105 | 106 | /** 107 | * Called via input to turn look up/down at a given rate. 108 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 109 | */ 110 | void LookUpAtRate(float Rate); 111 | 112 | struct TouchData 113 | { 114 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 115 | bool bIsPressed; 116 | ETouchIndex::Type FingerIndex; 117 | FVector Location; 118 | bool bMoved; 119 | }; 120 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 121 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 122 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 123 | TouchData TouchItem; 124 | 125 | protected: 126 | // APawn interface 127 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 128 | // End of APawn interface 129 | 130 | /* 131 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 132 | * 133 | * @param InputComponent The input component pointer to bind controls to 134 | * @returns true if touch controls were enabled. 135 | */ 136 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 137 | 138 | public: 139 | /** Returns Mesh1P subobject **/ 140 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 141 | /** Returns FirstPersonCameraComponent subobject **/ 142 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 143 | 144 | }; 145 | -------------------------------------------------------------------------------- /CharacterOverlapEvents/UnrealCPPCharacter.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Character.h" 7 | #include "UnrealCPPCharacter.generated.h" 8 | 9 | class UInputComponent; 10 | 11 | UCLASS(config=Game) 12 | class AUnrealCPPCharacter : public ACharacter 13 | { 14 | GENERATED_BODY() 15 | 16 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 17 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 18 | class USkeletalMeshComponent* Mesh1P; 19 | 20 | /** Gun mesh: 1st person view (seen only by self) */ 21 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 22 | class USkeletalMeshComponent* FP_Gun; 23 | 24 | /** Location on gun mesh where projectiles should spawn. */ 25 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 26 | class USceneComponent* FP_MuzzleLocation; 27 | 28 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 29 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 30 | class USkeletalMeshComponent* VR_Gun; 31 | 32 | /** Location on VR gun mesh where projectiles should spawn. */ 33 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 34 | class USceneComponent* VR_MuzzleLocation; 35 | 36 | /** First person camera */ 37 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 38 | class UCameraComponent* FirstPersonCameraComponent; 39 | 40 | /** Motion controller (right hand) */ 41 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 42 | class UMotionControllerComponent* R_MotionController; 43 | 44 | /** Motion controller (left hand) */ 45 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 46 | class UMotionControllerComponent* L_MotionController; 47 | 48 | public: 49 | AUnrealCPPCharacter(); 50 | 51 | protected: 52 | virtual void BeginPlay(); 53 | 54 | public: 55 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 56 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 57 | float BaseTurnRate; 58 | 59 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 60 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 61 | float BaseLookUpRate; 62 | 63 | /** Gun muzzle's offset from the characters location */ 64 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 65 | FVector GunOffset; 66 | 67 | /** Projectile class to spawn */ 68 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 69 | TSubclassOf ProjectileClass; 70 | 71 | /** Sound to play each time we fire */ 72 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 73 | class USoundBase* FireSound; 74 | 75 | /** AnimMontage to play each time we fire */ 76 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 77 | class UAnimMontage* FireAnimation; 78 | 79 | /** Whether to use motion controller location for aiming. */ 80 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 81 | uint32 bUsingMotionControllers : 1; 82 | 83 | UPROPERTY(VisibleAnywhere, Category = "Trigger Capsule") 84 | class UCapsuleComponent* TriggerCapsule; 85 | 86 | protected: 87 | 88 | /** Fires a projectile. */ 89 | void OnFire(); 90 | 91 | /** Resets HMD orientation and position in VR. */ 92 | void OnResetVR(); 93 | 94 | /** Handles moving forward/backward */ 95 | void MoveForward(float Val); 96 | 97 | /** Handles stafing movement, left and right */ 98 | void MoveRight(float Val); 99 | 100 | /** 101 | * Called via input to turn at a given rate. 102 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 103 | */ 104 | void TurnAtRate(float Rate); 105 | 106 | /** 107 | * Called via input to turn look up/down at a given rate. 108 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 109 | */ 110 | void LookUpAtRate(float Rate); 111 | 112 | struct TouchData 113 | { 114 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 115 | bool bIsPressed; 116 | ETouchIndex::Type FingerIndex; 117 | FVector Location; 118 | bool bMoved; 119 | }; 120 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 121 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 122 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 123 | TouchData TouchItem; 124 | 125 | // declare overlap begin function 126 | UFUNCTION() 127 | void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); 128 | 129 | // declare overlap end function 130 | UFUNCTION() 131 | void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); 132 | 133 | protected: 134 | // APawn interface 135 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 136 | // End of APawn interface 137 | 138 | /* 139 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 140 | * 141 | * @param InputComponent The input component pointer to bind controls to 142 | * @returns true if touch controls were enabled. 143 | */ 144 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 145 | 146 | public: 147 | /** Returns Mesh1P subobject **/ 148 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 149 | /** Returns FirstPersonCameraComponent subobject **/ 150 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 151 | 152 | }; 153 | -------------------------------------------------------------------------------- /CollidingPawn/CollidingPawn.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "CollidingPawn.h" 8 | #include "Camera/CameraComponent.h" 9 | #include "CollidingPawnMovementComponent.h" 10 | #include "Components/InputComponent.h" 11 | #include "Components/StaticMeshComponent.h" 12 | #include "Components/SphereComponent.h" 13 | #include "ConstructorHelpers.h" 14 | #include "GameFramework/SpringArmComponent.h" 15 | #include "Particles/ParticleSystemComponent.h" 16 | 17 | 18 | // Sets default values 19 | ACollidingPawn::ACollidingPawn() 20 | { 21 | // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. 22 | PrimaryActorTick.bCanEverTick = true; 23 | 24 | // Our root component will be a sphere that reacts to physics 25 | USphereComponent* SphereComponent = CreateDefaultSubobject(TEXT("RootComponent")); 26 | RootComponent = SphereComponent; 27 | SphereComponent->InitSphereRadius(40.0f); 28 | SphereComponent->SetCollisionProfileName(TEXT("Pawn")); 29 | 30 | // Create and position a mesh component so we can see where our sphere is 31 | UStaticMeshComponent* SphereVisual = CreateDefaultSubobject(TEXT("VisualRepresentation")); 32 | SphereVisual->SetupAttachment(RootComponent); 33 | static ConstructorHelpers::FObjectFinder SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere")); 34 | if (SphereVisualAsset.Succeeded()) 35 | { 36 | SphereVisual->SetStaticMesh(SphereVisualAsset.Object); 37 | SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f)); 38 | SphereVisual->SetWorldScale3D(FVector(0.8f)); 39 | } 40 | 41 | // Create a particle system that we can activate or deactivate 42 | OurParticleSystem = CreateDefaultSubobject(TEXT("MovementParticles")); 43 | OurParticleSystem->SetupAttachment(SphereVisual); 44 | OurParticleSystem->bAutoActivate = false; 45 | OurParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f)); 46 | static ConstructorHelpers::FObjectFinder ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire")); 47 | if (ParticleAsset.Succeeded()) 48 | { 49 | OurParticleSystem->SetTemplate(ParticleAsset.Object); 50 | } 51 | 52 | // Use a spring arm to give the camera smooth, natural-feeling motion. 53 | USpringArmComponent* SpringArm = CreateDefaultSubobject(TEXT("CameraAttachmentArm")); 54 | SpringArm->SetupAttachment(RootComponent); 55 | SpringArm->RelativeRotation = FRotator(-45.f, 0.f, 0.f); 56 | SpringArm->TargetArmLength = 400.0f; 57 | SpringArm->bEnableCameraLag = true; 58 | SpringArm->CameraLagSpeed = 3.0f; 59 | 60 | // Create a camera and attach to our spring arm 61 | UCameraComponent* Camera = CreateDefaultSubobject(TEXT("ActualCamera")); 62 | Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName); 63 | 64 | // Take control of the default player 65 | AutoPossessPlayer = EAutoReceiveInput::Player0; 66 | 67 | // Create an instance of our movement component, and tell it to update our root component. 68 | OurMovementComponent = CreateDefaultSubobject(TEXT("CustomMovementComponent")); 69 | OurMovementComponent->UpdatedComponent = RootComponent; 70 | 71 | } 72 | 73 | // Called every frame 74 | void ACollidingPawn::Tick(float DeltaTime) 75 | { 76 | Super::Tick(DeltaTime); 77 | 78 | } 79 | 80 | // Called to bind functionality to input 81 | void ACollidingPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent) 82 | { 83 | Super::SetupPlayerInputComponent(InputComponent); 84 | 85 | InputComponent->BindAction("ParticleToggle", IE_Pressed, this, &ACollidingPawn::ParticleToggle); 86 | 87 | InputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward); 88 | InputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight); 89 | InputComponent->BindAxis("Turn", this, &ACollidingPawn::Turn); 90 | } 91 | 92 | UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const 93 | { 94 | return OurMovementComponent; 95 | } 96 | 97 | void ACollidingPawn::MoveForward(float AxisValue) 98 | { 99 | if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent)) 100 | { 101 | OurMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue); 102 | } 103 | } 104 | 105 | void ACollidingPawn::MoveRight(float AxisValue) 106 | { 107 | if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent)) 108 | { 109 | OurMovementComponent->AddInputVector(GetActorRightVector() * AxisValue); 110 | } 111 | } 112 | 113 | void ACollidingPawn::Turn(float AxisValue) 114 | { 115 | FRotator NewRotation = GetActorRotation(); 116 | NewRotation.Yaw += AxisValue; 117 | SetActorRotation(NewRotation); 118 | } 119 | 120 | void ACollidingPawn::ParticleToggle() 121 | { 122 | if (OurParticleSystem && OurParticleSystem->Template) 123 | { 124 | OurParticleSystem->ToggleActive(); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /CollidingPawn/CollidingPawn.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Pawn.h" 10 | #include "CollidingPawn.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ACollidingPawn : public APawn 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this pawn's properties 19 | ACollidingPawn(); 20 | 21 | public: 22 | // Called every frame 23 | virtual void Tick( float DeltaSeconds ) override; 24 | 25 | // Called to bind functionality to input 26 | virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override; 27 | 28 | UParticleSystemComponent* OurParticleSystem; 29 | class UCollidingPawnMovementComponent* OurMovementComponent; 30 | 31 | virtual UPawnMovementComponent* GetMovementComponent() const override; 32 | 33 | void MoveForward(float AxisValue); 34 | void MoveRight(float AxisValue); 35 | void Turn(float AxisValue); 36 | void ParticleToggle(); 37 | }; 38 | -------------------------------------------------------------------------------- /CollidingPawn/CollidingPawnMovementComponent.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "CollidingPawnMovementComponent.h" 8 | 9 | void UCollidingPawnMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) 10 | { 11 | Super::TickComponent(DeltaTime, TickType, ThisTickFunction); 12 | 13 | // Make sure that everything is still valid, and that we are allowed to move. 14 | if (!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(DeltaTime)) 15 | { 16 | return; 17 | } 18 | 19 | // Get (and then clear) the movement vector that we set in ACollidingPawn::Tick 20 | FVector DesiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * 150.0f; 21 | if (!DesiredMovementThisFrame.IsNearlyZero()) 22 | { 23 | FHitResult Hit; 24 | SafeMoveUpdatedComponent(DesiredMovementThisFrame, UpdatedComponent->GetComponentRotation(), true, Hit); 25 | 26 | // If we bumped into something, try to slide along it 27 | if (Hit.IsValidBlockingHit()) 28 | { 29 | SlideAlongSurface(DesiredMovementThisFrame, 1.f - Hit.Time, Hit.Normal, Hit); 30 | } 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /CollidingPawn/CollidingPawnMovementComponent.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/PawnMovementComponent.h" 10 | #include "CollidingPawnMovementComponent.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API UCollidingPawnMovementComponent : public UPawnMovementComponent 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override; 19 | }; 20 | -------------------------------------------------------------------------------- /ConsoleLog/ConsoleLog.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | // define a print message function to print to screen 8 | #define print(text) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green,text) 9 | #define printFString(text, fstring) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT(text), fstring)) 10 | 11 | #include "ConsoleLog.h" 12 | 13 | // Sets default values 14 | AConsoleLog::AConsoleLog() 15 | { 16 | DefaultSceneComponent = CreateDefaultSubobject(TEXT("Default Scene Component")); 17 | RootComponent = DefaultSceneComponent; 18 | } 19 | 20 | // Called when the game starts or when spawned 21 | void AConsoleLog::BeginPlay() 22 | { 23 | Super::BeginPlay(); 24 | 25 | // Standard way to log to console. 26 | UE_LOG(LogTemp, Warning, TEXT("I just started running")); 27 | 28 | // Log to Screen 29 | if(GEngine) 30 | { 31 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Screen Message")); 32 | } 33 | 34 | FVector MyVector = FVector(200.0f, 100.0f, 900.0f); 35 | 36 | // log vector 37 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, FString::Printf(TEXT("My Location is: %s"), *MyVector.ToString())); 38 | 39 | // Use the shortcut defined above 40 | print("Hello Unreal"); 41 | printFString("My Variable Vector is: %s", *MyVector.ToString()); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /ConsoleLog/ConsoleLog.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "ConsoleLog.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AConsoleLog : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | AConsoleLog(); 20 | 21 | UPROPERTY(EditAnywhere) 22 | class USceneComponent* DefaultSceneComponent; 23 | 24 | protected: 25 | // Called when the game starts or when spawned 26 | virtual void BeginPlay() override; 27 | }; 28 | -------------------------------------------------------------------------------- /CreateStaticMesh/CreateStaticMesh.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "CreateStaticMesh.h" 8 | 9 | 10 | // Sets default values 11 | ACreateStaticMesh::ACreateStaticMesh() 12 | { 13 | // Add static mesh component to actor 14 | SuperMesh = CreateDefaultSubobject(TEXT("My Super Mesh")); 15 | } 16 | -------------------------------------------------------------------------------- /CreateStaticMesh/CreateStaticMesh.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "CreateStaticMesh.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ACreateStaticMesh : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | ACreateStaticMesh(); 20 | 21 | UPROPERTY(VisibleAnywhere) 22 | UStaticMeshComponent* SuperMesh; 23 | }; 24 | -------------------------------------------------------------------------------- /DefaultStarterFiles/UnrealCPP.Build.cs.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | using UnrealBuildTool; 4 | 5 | public class UnrealCPP : ModuleRules 6 | { 7 | public UnrealCPP(ReadOnlyTargetRules Target) : base(Target) 8 | { 9 | PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; 10 | 11 | PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay" }); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /DefaultStarterFiles/UnrealCPP.cpp.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealCPP.h" 4 | #include "Modules/ModuleManager.h" 5 | 6 | IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, UnrealCPP, "UnrealCPP" ); 7 | -------------------------------------------------------------------------------- /DefaultStarterFiles/UnrealCPP.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | -------------------------------------------------------------------------------- /DefaultStarterFiles/UnrealCPPCharacter.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Character.h" 7 | #include "UnrealCPPCharacter.generated.h" 8 | 9 | class UInputComponent; 10 | 11 | UCLASS(config=Game) 12 | class AUnrealCPPCharacter : public ACharacter 13 | { 14 | GENERATED_BODY() 15 | 16 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 17 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 18 | class USkeletalMeshComponent* Mesh1P; 19 | 20 | /** Gun mesh: 1st person view (seen only by self) */ 21 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 22 | class USkeletalMeshComponent* FP_Gun; 23 | 24 | /** Location on gun mesh where projectiles should spawn. */ 25 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 26 | class USceneComponent* FP_MuzzleLocation; 27 | 28 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 29 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 30 | class USkeletalMeshComponent* VR_Gun; 31 | 32 | /** Location on VR gun mesh where projectiles should spawn. */ 33 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 34 | class USceneComponent* VR_MuzzleLocation; 35 | 36 | /** First person camera */ 37 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 38 | class UCameraComponent* FirstPersonCameraComponent; 39 | 40 | /** Motion controller (right hand) */ 41 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 42 | class UMotionControllerComponent* R_MotionController; 43 | 44 | /** Motion controller (left hand) */ 45 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 46 | class UMotionControllerComponent* L_MotionController; 47 | 48 | public: 49 | AUnrealCPPCharacter(); 50 | 51 | protected: 52 | virtual void BeginPlay(); 53 | 54 | public: 55 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 56 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 57 | float BaseTurnRate; 58 | 59 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 60 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 61 | float BaseLookUpRate; 62 | 63 | /** Gun muzzle's offset from the characters location */ 64 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 65 | FVector GunOffset; 66 | 67 | /** Projectile class to spawn */ 68 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 69 | TSubclassOf ProjectileClass; 70 | 71 | /** Sound to play each time we fire */ 72 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 73 | class USoundBase* FireSound; 74 | 75 | /** AnimMontage to play each time we fire */ 76 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 77 | class UAnimMontage* FireAnimation; 78 | 79 | /** Whether to use motion controller location for aiming. */ 80 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 81 | uint32 bUsingMotionControllers : 1; 82 | 83 | protected: 84 | 85 | /** Fires a projectile. */ 86 | void OnFire(); 87 | 88 | /** Resets HMD orientation and position in VR. */ 89 | void OnResetVR(); 90 | 91 | /** Handles moving forward/backward */ 92 | void MoveForward(float Val); 93 | 94 | /** Handles stafing movement, left and right */ 95 | void MoveRight(float Val); 96 | 97 | /** 98 | * Called via input to turn at a given rate. 99 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 100 | */ 101 | void TurnAtRate(float Rate); 102 | 103 | /** 104 | * Called via input to turn look up/down at a given rate. 105 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 106 | */ 107 | void LookUpAtRate(float Rate); 108 | 109 | struct TouchData 110 | { 111 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 112 | bool bIsPressed; 113 | ETouchIndex::Type FingerIndex; 114 | FVector Location; 115 | bool bMoved; 116 | }; 117 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 118 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 119 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 120 | TouchData TouchItem; 121 | 122 | protected: 123 | // APawn interface 124 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 125 | // End of APawn interface 126 | 127 | /* 128 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 129 | * 130 | * @param InputComponent The input component pointer to bind controls to 131 | * @returns true if touch controls were enabled. 132 | */ 133 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 134 | 135 | public: 136 | /** Returns Mesh1P subobject **/ 137 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 138 | /** Returns FirstPersonCameraComponent subobject **/ 139 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 140 | 141 | }; 142 | -------------------------------------------------------------------------------- /DefaultStarterFiles/UnrealCPPGameMode.cpp.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealCPPGameMode.h" 4 | #include "UnrealCPPHUD.h" 5 | #include "UnrealCPPCharacter.h" 6 | #include "UObject/ConstructorHelpers.h" 7 | 8 | AUnrealCPPGameMode::AUnrealCPPGameMode() 9 | : Super() 10 | { 11 | // set default pawn class to our Blueprinted character 12 | static ConstructorHelpers::FClassFinder PlayerPawnClassFinder(TEXT("/Game/FirstPersonCPP/Blueprints/FirstPersonCharacter")); 13 | DefaultPawnClass = PlayerPawnClassFinder.Class; 14 | 15 | // use our custom HUD class 16 | HUDClass = AUnrealCPPHUD::StaticClass(); 17 | } 18 | -------------------------------------------------------------------------------- /DefaultStarterFiles/UnrealCPPGameMode.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/GameModeBase.h" 7 | #include "UnrealCPPGameMode.generated.h" 8 | 9 | UCLASS(minimalapi) 10 | class AUnrealCPPGameMode : public AGameModeBase 11 | { 12 | GENERATED_BODY() 13 | 14 | public: 15 | AUnrealCPPGameMode(); 16 | }; 17 | -------------------------------------------------------------------------------- /DefaultStarterFiles/UnrealCPPHUD.cpp.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealCPPHUD.h" 4 | #include "Engine/Canvas.h" 5 | #include "Engine/Texture2D.h" 6 | #include "TextureResource.h" 7 | #include "CanvasItem.h" 8 | #include "UObject/ConstructorHelpers.h" 9 | 10 | AUnrealCPPHUD::AUnrealCPPHUD() 11 | { 12 | // Set the crosshair texture 13 | static ConstructorHelpers::FObjectFinder CrosshairTexObj(TEXT("/Game/FirstPerson/Textures/FirstPersonCrosshair")); 14 | CrosshairTex = CrosshairTexObj.Object; 15 | } 16 | 17 | 18 | void AUnrealCPPHUD::DrawHUD() 19 | { 20 | Super::DrawHUD(); 21 | 22 | // Draw very simple crosshair 23 | 24 | // find center of the Canvas 25 | const FVector2D Center(Canvas->ClipX * 0.5f, Canvas->ClipY * 0.5f); 26 | 27 | // offset by half the texture's dimensions so that the center of the texture aligns with the center of the Canvas 28 | const FVector2D CrosshairDrawPosition( (Center.X), 29 | (Center.Y + 20.0f)); 30 | 31 | // draw the crosshair 32 | FCanvasTileItem TileItem( CrosshairDrawPosition, CrosshairTex->Resource, FLinearColor::White); 33 | TileItem.BlendMode = SE_BLEND_Translucent; 34 | Canvas->DrawItem( TileItem ); 35 | } 36 | -------------------------------------------------------------------------------- /DefaultStarterFiles/UnrealCPPHUD.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/HUD.h" 7 | #include "UnrealCPPHUD.generated.h" 8 | 9 | UCLASS() 10 | class AUnrealCPPHUD : public AHUD 11 | { 12 | GENERATED_BODY() 13 | 14 | public: 15 | AUnrealCPPHUD(); 16 | 17 | /** Primary draw call for the HUD */ 18 | virtual void DrawHUD() override; 19 | 20 | private: 21 | /** Crosshair asset pointer */ 22 | class UTexture2D* CrosshairTex; 23 | 24 | }; 25 | -------------------------------------------------------------------------------- /DefaultStarterFiles/UnrealCPPProjectile.cpp.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealCPPProjectile.h" 4 | #include "GameFramework/ProjectileMovementComponent.h" 5 | #include "Components/SphereComponent.h" 6 | 7 | AUnrealCPPProjectile::AUnrealCPPProjectile() 8 | { 9 | // Use a sphere as a simple collision representation 10 | CollisionComp = CreateDefaultSubobject(TEXT("SphereComp")); 11 | CollisionComp->InitSphereRadius(5.0f); 12 | CollisionComp->BodyInstance.SetCollisionProfileName("Projectile"); 13 | CollisionComp->OnComponentHit.AddDynamic(this, &AUnrealCPPProjectile::OnHit); // set up a notification for when this component hits something blocking 14 | 15 | // Players can't walk on it 16 | CollisionComp->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f)); 17 | CollisionComp->CanCharacterStepUpOn = ECB_No; 18 | 19 | // Set as root component 20 | RootComponent = CollisionComp; 21 | 22 | // Use a ProjectileMovementComponent to govern this projectile's movement 23 | ProjectileMovement = CreateDefaultSubobject(TEXT("ProjectileComp")); 24 | ProjectileMovement->UpdatedComponent = CollisionComp; 25 | ProjectileMovement->InitialSpeed = 3000.f; 26 | ProjectileMovement->MaxSpeed = 3000.f; 27 | ProjectileMovement->bRotationFollowsVelocity = true; 28 | ProjectileMovement->bShouldBounce = true; 29 | 30 | // Die after 3 seconds by default 31 | InitialLifeSpan = 3.0f; 32 | } 33 | 34 | void AUnrealCPPProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) 35 | { 36 | // Only add impulse and destroy projectile if we hit a physics 37 | if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL) && OtherComp->IsSimulatingPhysics()) 38 | { 39 | OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation()); 40 | 41 | Destroy(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /DefaultStarterFiles/UnrealCPPProjectile.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Actor.h" 7 | #include "UnrealCPPProjectile.generated.h" 8 | 9 | UCLASS(config=Game) 10 | class AUnrealCPPProjectile : public AActor 11 | { 12 | GENERATED_BODY() 13 | 14 | /** Sphere collision component */ 15 | UPROPERTY(VisibleDefaultsOnly, Category=Projectile) 16 | class USphereComponent* CollisionComp; 17 | 18 | /** Projectile movement component */ 19 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true")) 20 | class UProjectileMovementComponent* ProjectileMovement; 21 | 22 | public: 23 | AUnrealCPPProjectile(); 24 | 25 | /** called when projectile hits something */ 26 | UFUNCTION() 27 | void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit); 28 | 29 | /** Returns CollisionComp subobject **/ 30 | FORCEINLINE class USphereComponent* GetCollisionComp() const { return CollisionComp; } 31 | /** Returns ProjectileMovement subobject **/ 32 | FORCEINLINE class UProjectileMovementComponent* GetProjectileMovement() const { return ProjectileMovement; } 33 | }; 34 | -------------------------------------------------------------------------------- /DestroyActorOnOverlap/DestroyActorOnOverlap.cpp: -------------------------------------------------------------------------------- 1 | // Unreal Version 4.25.1 2 | 3 | 4 | #include "DestroyActorOnOverlap.h" 5 | #include "Components/SphereComponent.h" 6 | 7 | // Sets default values 8 | ADestroyActorOnOverlap::ADestroyActorOnOverlap() 9 | { 10 | MySphereComponent = CreateDefaultSubobject(TEXT("My Sphere Component")); 11 | MySphereComponent->InitSphereRadius(100.f); 12 | MySphereComponent->SetCollisionProfileName(TEXT("Trigger")); 13 | RootComponent = MySphereComponent; 14 | 15 | MySphereComponent->OnComponentBeginOverlap.AddDynamic(this, &ADestroyActorOnOverlap::OnOverlapBegin); 16 | } 17 | 18 | void ADestroyActorOnOverlap::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) 19 | { 20 | Destroy(); 21 | } 22 | -------------------------------------------------------------------------------- /DestroyActorOnOverlap/DestroyActorOnOverlap.h: -------------------------------------------------------------------------------- 1 | // Unreal Version 4.25.1 2 | 3 | 4 | #pragma once 5 | 6 | #include "CoreMinimal.h" 7 | #include "GameFramework/Actor.h" 8 | #include "DestroyActorOnOverlap.generated.h" 9 | 10 | UCLASS() 11 | class UNREALCPP_API ADestroyActorOnOverlap : public AActor 12 | { 13 | GENERATED_BODY() 14 | 15 | public: 16 | // Sets default values for this actor's properties 17 | ADestroyActorOnOverlap(); 18 | 19 | UPROPERTY(VisibleAnywhere) 20 | class USphereComponent* MySphereComponent; 21 | 22 | UFUNCTION() 23 | void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); 24 | }; 25 | -------------------------------------------------------------------------------- /FindPlayerPosition/BP_FindPlayerPosition.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harrison1/unrealcpp/cfcb8611ab5c67dc3dc85ece3a4c13720d6c17b9/FindPlayerPosition/BP_FindPlayerPosition.png -------------------------------------------------------------------------------- /FindPlayerPosition/FindPlayerPosition.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "FindPlayerPosition.h" 8 | 9 | 10 | // Sets default values 11 | AFindPlayerPosition::AFindPlayerPosition() 12 | { 13 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 14 | PrimaryActorTick.bCanEverTick = true; 15 | } 16 | 17 | // Called every frame 18 | void AFindPlayerPosition::Tick(float DeltaTime) 19 | { 20 | Super::Tick(DeltaTime); 21 | 22 | // get first player pawn location 23 | FVector MyCharacter = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation(); 24 | 25 | // screen log player location 26 | if(GEngine) 27 | { 28 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Player Location: %s"), *MyCharacter.ToString())); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /FindPlayerPosition/FindPlayerPosition.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "FindPlayerPosition.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AFindPlayerPosition : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | AFindPlayerPosition(); 20 | 21 | // Called every frame 22 | virtual void Tick(float DeltaTime) override; 23 | 24 | }; 25 | -------------------------------------------------------------------------------- /FloatingActor/FloatingActor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "FloatingActor.h" 8 | 9 | 10 | // Sets default values 11 | AFloatingActor::AFloatingActor() 12 | { 13 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 14 | PrimaryActorTick.bCanEverTick = true; 15 | } 16 | 17 | // Called every frame 18 | void AFloatingActor::Tick(float DeltaTime) 19 | { 20 | Super::Tick(DeltaTime); 21 | 22 | // on every frame change location for a smooth floating actor 23 | FVector NewLocation = GetActorLocation(); 24 | float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime)); 25 | NewLocation.X += DeltaHeight * XValue; 26 | NewLocation.Y += DeltaHeight * YValue; 27 | NewLocation.Z += DeltaHeight * ZValue; 28 | 29 | RunningTime += DeltaTime; 30 | SetActorLocation(NewLocation); 31 | } 32 | -------------------------------------------------------------------------------- /FloatingActor/FloatingActor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "FloatingActor.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AFloatingActor : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | AFloatingActor(); 20 | 21 | // Called every frame 22 | virtual void Tick(float DeltaTime) override; 23 | 24 | // declare our float variables 25 | float RunningTime; 26 | 27 | UPROPERTY(EditAnywhere, Category = Movement) 28 | float XValue; 29 | 30 | UPROPERTY(EditAnywhere, Category = Movement) 31 | float YValue; 32 | 33 | UPROPERTY(EditAnywhere, Category = Movement) 34 | float ZValue; 35 | }; 36 | -------------------------------------------------------------------------------- /GetNumberOfPawns/GetNumberOfPawns.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "GetNumberOfPawns.h" 8 | 9 | // Called when the game starts or when spawned 10 | void AGetNumberOfPawns::BeginPlay() 11 | { 12 | Super::BeginPlay(); 13 | 14 | int32 MyPawns = GetWorld()->GetNumPawns(); 15 | 16 | if(GEngine) 17 | { 18 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Number of Pawns: %d"), MyPawns)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /GetNumberOfPawns/GetNumberOfPawns.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "GetNumberOfPawns.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AGetNumberOfPawns : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | protected: 18 | // Called when the game starts or when spawned 19 | virtual void BeginPlay() override; 20 | }; 21 | -------------------------------------------------------------------------------- /HealthBar/CampFire.cpp.txt: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "CampFire.h" 8 | #include "Kismet/GameplayStatics.h" 9 | #include "TimerManager.h" 10 | #include "Components/BoxComponent.h" 11 | #include "Particles/ParticleSystemComponent.h" 12 | 13 | // Sets default values 14 | ACampFire::ACampFire() 15 | { 16 | MyBoxComponent = CreateDefaultSubobject(TEXT("My Box Component")); 17 | MyBoxComponent->InitBoxExtent(FVector(50.0f,50.0f,50.0f)); 18 | RootComponent = MyBoxComponent; 19 | 20 | Fire = CreateDefaultSubobject(TEXT("My Fire")); 21 | Fire->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f)); 22 | Fire->SetupAttachment(RootComponent); 23 | 24 | MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &ACampFire::OnOverlapBegin); 25 | MyBoxComponent->OnComponentEndOverlap.AddDynamic(this, &ACampFire::OnOverlapEnd); 26 | 27 | bCanApplyDamage = false; 28 | } 29 | 30 | void ACampFire::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) 31 | { 32 | if ( (OtherActor != nullptr ) && (OtherActor != this) && ( OtherComp != nullptr ) ) 33 | { 34 | bCanApplyDamage = true; 35 | MyCharacter = Cast(OtherActor); 36 | MyHit = SweepResult; 37 | GetWorldTimerManager().SetTimer(FireTimerHandle, this, &ACampFire::ApplyFireDamage, 2.2f, true, 0.0f); 38 | } 39 | } 40 | 41 | void ACampFire::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) 42 | { 43 | bCanApplyDamage = false; 44 | GetWorldTimerManager().ClearTimer(FireTimerHandle); 45 | } 46 | 47 | void ACampFire::ApplyFireDamage() 48 | { 49 | if(bCanApplyDamage) 50 | { 51 | UGameplayStatics::ApplyPointDamage(MyCharacter, 200.0f, GetActorLocation(), MyHit, nullptr, this, FireDamageType); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /HealthBar/CampFire.h.txt: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "CampFire.generated.h" 11 | 12 | class UBoxComponent; 13 | class UParticleSystemComponent; 14 | 15 | UCLASS() 16 | class UNREALCPP_API ACampFire : public AActor 17 | { 18 | GENERATED_BODY() 19 | 20 | public: 21 | // Sets default values for this actor's properties 22 | ACampFire(); 23 | 24 | public: 25 | 26 | UPROPERTY(EditAnywhere) 27 | UParticleSystemComponent* Fire; 28 | 29 | UPROPERTY(EditAnywhere) 30 | UBoxComponent* MyBoxComponent; 31 | 32 | UPROPERTY(EditAnywhere) 33 | TSubclassOf FireDamageType; 34 | 35 | UPROPERTY(EditAnywhere) 36 | AActor* MyCharacter; 37 | 38 | UPROPERTY(EditAnywhere) 39 | FHitResult MyHit; 40 | 41 | bool bCanApplyDamage; 42 | FTimerHandle FireTimerHandle; 43 | 44 | // declare component overlap begin function 45 | UFUNCTION() 46 | void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); 47 | 48 | // declare component overlap end function 49 | UFUNCTION() 50 | void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); 51 | 52 | UFUNCTION() 53 | void ApplyFireDamage(); 54 | }; 55 | -------------------------------------------------------------------------------- /HealthBar/MedKit.cpp.txt: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MedKit.h" 8 | #include "UnrealCPPCharacter.h" 9 | 10 | // Sets default values 11 | AMedKit::AMedKit() 12 | { 13 | OnActorBeginOverlap.AddDynamic(this, &AMedKit::OnOverlap); 14 | } 15 | 16 | void AMedKit::OnOverlap(AActor* MyOverlappedActor, AActor* OtherActor) 17 | { 18 | if (OtherActor != nullptr && OtherActor != this) 19 | { 20 | class AUnrealCPPCharacter* MyCharacter = Cast(OtherActor); 21 | 22 | if (MyCharacter && MyCharacter->GetHealth() < 1.0f) 23 | { 24 | MyCharacter->UpdateHealth(100.0f); 25 | Destroy(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /HealthBar/MedKit.h.txt: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "MedKit.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AMedKit : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | AMedKit(); 20 | 21 | UFUNCTION() 22 | void OnOverlap(AActor* MyOverlappedActor, AActor* OtherActor); 23 | 24 | }; 25 | -------------------------------------------------------------------------------- /HealthBar/UnrealCPP.Build.cs.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | using UnrealBuildTool; 4 | 5 | public class UnrealCPP : ModuleRules 6 | { 7 | public UnrealCPP(ReadOnlyTargetRules Target) : base(Target) 8 | { 9 | PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; 10 | 11 | PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "UMG", "Slate", "SlateCore" }); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HealthBar/UnrealCPPGameMode.cpp.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealCPPGameMode.h" 4 | #include "UnrealCPPHUD.h" 5 | #include "Kismet/GameplayStatics.h" 6 | #include "UnrealCPPCharacter.h" 7 | #include "UObject/ConstructorHelpers.h" 8 | #include "Blueprint/UserWidget.h" 9 | #include "UnrealCPPCharacter.h" 10 | 11 | AUnrealCPPGameMode::AUnrealCPPGameMode() 12 | : Super() 13 | { 14 | PrimaryActorTick.bCanEverTick = true; 15 | 16 | // set default pawn class to our Blueprinted character 17 | static ConstructorHelpers::FClassFinder PlayerPawnClassFinder(TEXT("/Game/FirstPersonCPP/Blueprints/FirstPersonCharacter")); 18 | DefaultPawnClass = PlayerPawnClassFinder.Class; 19 | 20 | static ConstructorHelpers::FClassFinder HealthBar(TEXT("/Game/FirstPerson/UI/Health_UI")); 21 | HUDWidgetClass = HealthBar.Class; 22 | 23 | // use our custom HUD class 24 | HUDClass = AUnrealCPPHUD::StaticClass(); 25 | 26 | // add Health Bar UI to viewport 27 | if (HUDWidgetClass != nullptr) 28 | { 29 | CurrentWidget = CreateWidget(GetWorld(), HUDWidgetClass); 30 | 31 | if (CurrentWidget) 32 | { 33 | CurrentWidget->AddToViewport(); 34 | } 35 | } 36 | } 37 | 38 | void AUnrealCPPGameMode::BeginPlay() 39 | { 40 | Super::BeginPlay(); 41 | 42 | SetCurrentState(EGamePlayState::EPlaying); 43 | 44 | MyCharacter = Cast(UGameplayStatics::GetPlayerPawn(this, 0)); 45 | } 46 | 47 | void AUnrealCPPGameMode::Tick(float DeltaTime) 48 | { 49 | Super::Tick(DeltaTime); 50 | 51 | if (MyCharacter) 52 | { 53 | if (FMath::IsNearlyZero(MyCharacter->GetHealth(), 0.001f)) 54 | { 55 | SetCurrentState(EGamePlayState::EGameOver); 56 | } 57 | } 58 | } 59 | 60 | EGamePlayState AUnrealCPPGameMode::GetCurrentState() const 61 | { 62 | return CurrentState; 63 | } 64 | 65 | void AUnrealCPPGameMode::SetCurrentState(EGamePlayState NewState) 66 | { 67 | CurrentState = NewState; 68 | HandleNewState(CurrentState); 69 | } 70 | 71 | void AUnrealCPPGameMode::HandleNewState(EGamePlayState NewState) 72 | { 73 | switch (NewState) 74 | { 75 | case EGamePlayState::EPlaying: 76 | { 77 | // do nothing 78 | } 79 | break; 80 | // Unknown/default state 81 | case EGamePlayState::EGameOver: 82 | { 83 | UGameplayStatics::OpenLevel(this, FName(*GetWorld()->GetName()), false); 84 | } 85 | break; 86 | // Unknown/default state 87 | default: 88 | case EGamePlayState::EUnknown: 89 | { 90 | // do nothing 91 | } 92 | break; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /HealthBar/UnrealCPPGameMode.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "GameFramework/GameModeBase.h" 6 | #include "UnrealCPPGameMode.generated.h" 7 | 8 | class AUnrealCPPCharacter; 9 | 10 | //enum to store the current state of gameplay 11 | UENUM() 12 | enum class EGamePlayState 13 | { 14 | EPlaying, 15 | EGameOver, 16 | EUnknown 17 | }; 18 | 19 | UCLASS(minimalapi) 20 | class AUnrealCPPGameMode : public AGameModeBase 21 | { 22 | GENERATED_BODY() 23 | 24 | public: 25 | AUnrealCPPGameMode(); 26 | 27 | virtual void BeginPlay() override; 28 | 29 | virtual void Tick(float DeltaTime) override; 30 | 31 | AUnrealCPPCharacter* MyCharacter; 32 | 33 | /** Returns the current playing state */ 34 | UFUNCTION(BlueprintPure, Category = "Health") 35 | EGamePlayState GetCurrentState() const; 36 | 37 | /** Sets a new playing state */ 38 | void SetCurrentState(EGamePlayState NewState); 39 | 40 | UPROPERTY(EditAnywhere, Category = "Health") 41 | TSubclassOf HUDWidgetClass; 42 | 43 | UPROPERTY(EditAnywhere, Category = "Health") 44 | class UUserWidget* CurrentWidget; 45 | 46 | private: 47 | /**Keeps track of the current playing state */ 48 | EGamePlayState CurrentState; 49 | 50 | /**Handle any function calls that rely upon changing the playing state of our game */ 51 | void HandleNewState(EGamePlayState NewState); 52 | }; 53 | -------------------------------------------------------------------------------- /HideActor/HideActor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "HideActor.h" 8 | 9 | // Called when the game starts or when spawned 10 | void AHideActor::BeginPlay() 11 | { 12 | Super::BeginPlay(); 13 | 14 | if (HideInGame) 15 | { 16 | DisableActor(HideInGame); 17 | } 18 | } 19 | 20 | void AHideActor::DisableActor(bool toHide) 21 | { 22 | // Hides visible components 23 | SetActorHiddenInGame(toHide); 24 | 25 | // Disables collision components 26 | SetActorEnableCollision(false); 27 | 28 | // Stops the Actor from ticking 29 | SetActorTickEnabled(false); 30 | } 31 | -------------------------------------------------------------------------------- /HideActor/HideActor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "HideActor.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AHideActor : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | protected: 18 | // Called when the game starts or when spawned 19 | virtual void BeginPlay() override; 20 | 21 | public: 22 | 23 | UPROPERTY(EditAnywhere, Category = "Disable") 24 | bool HideInGame; 25 | 26 | UFUNCTION(BlueprintCallable, Category = "Disable") 27 | void DisableActor(bool toHide); 28 | 29 | }; 30 | -------------------------------------------------------------------------------- /InventoryArray/InventoryItem.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.3 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "InventoryItem.h" 8 | 9 | // Sets default values 10 | AInventoryItem::AInventoryItem() 11 | { 12 | 13 | InventoryItemMesh = CreateDefaultSubobject(TEXT("InventoryItem Mesh")); 14 | InventoryItemMesh->SetSimulatePhysics(true); 15 | RootComponent = InventoryItemMesh; 16 | 17 | InventoryItemTexture = CreateDefaultSubobject(TEXT("InventoryItem Texture")); 18 | 19 | isGlowing = false; 20 | 21 | } 22 | 23 | // Called when the game starts or when spawned 24 | void AInventoryItem::BeginPlay() 25 | { 26 | Super::BeginPlay(); 27 | 28 | InitialMaterial = InventoryItemMesh->GetMaterial(0); 29 | 30 | DynamicMatInstance = InventoryItemMesh->CreateAndSetMaterialInstanceDynamic(0); 31 | 32 | if(InventoryItemMesh->GetStaticMesh() && DynamicMatInstance) 33 | { 34 | UE_LOG(LogTemp, Warning, TEXT("I have a mesh and material")); 35 | } 36 | else 37 | { 38 | UE_LOG(LogTemp, Warning, TEXT("I am missing either the mesh or material")); 39 | } 40 | } 41 | 42 | void AInventoryItem::BeginFocus() 43 | { 44 | if(InventoryItemMesh->GetStaticMesh() && DynamicMatInstance) 45 | { 46 | DynamicMatInstance->SetScalarParameterValue(FName("EmissiveParam"), 50.0f); 47 | } 48 | } 49 | 50 | void AInventoryItem::EndFocus() 51 | { 52 | if(InventoryItemMesh->GetStaticMesh() && DynamicMatInstance) 53 | { 54 | DynamicMatInstance->SetScalarParameterValue(FName("EmissiveParam"), 0.0f); 55 | } 56 | } 57 | 58 | FString AInventoryItem::MyName() 59 | { 60 | return InventoryItemName; 61 | } 62 | -------------------------------------------------------------------------------- /InventoryArray/InventoryItem.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.3 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "InventoryItem.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AInventoryItem : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | AInventoryItem(); 20 | 21 | protected: 22 | // Called when the game starts or when spawned 23 | virtual void BeginPlay() override; 24 | 25 | public: 26 | 27 | UPROPERTY(EditAnywhere, Category = InventoryItem) 28 | class UStaticMeshComponent* InventoryItemMesh; 29 | 30 | UPROPERTY(EditAnywhere, Category = InventoryItem) 31 | class UTexture2D* InventoryItemTexture; 32 | 33 | UPROPERTY(EditAnywhere, Category = InventoryItem) 34 | FString InventoryItemName; 35 | 36 | UPROPERTY(EditAnywhere, Category = InventoryItem) 37 | class UMaterialInterface* InitialMaterial; 38 | 39 | UPROPERTY(EditAnywhere, Category = InventoryItem) 40 | class UMaterialInterface* GlowMaterial; 41 | 42 | UPROPERTY(EditAnywhere, Category = InventoryItem) 43 | class UMaterialInstanceDynamic* DynamicMatInstance; 44 | 45 | UPROPERTY(EditAnywhere, Category = InventoryItem) 46 | bool isGlowing; 47 | 48 | UFUNCTION() 49 | void BeginFocus(); 50 | 51 | UFUNCTION() 52 | void EndFocus(); 53 | 54 | UFUNCTION() 55 | FString MyName(); 56 | 57 | FORCEINLINE UTexture2D* GetInventoryItemTexture() { return InventoryItemTexture; } 58 | 59 | }; 60 | 61 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Harrison 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LightSwitchPushButton/LightSwitchPushButton.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "LightSwitchPushButton.h" 8 | #include "Components/PointLightComponent.h" 9 | #include "Components/SphereComponent.h" 10 | 11 | // Sets default values 12 | ALightSwitchPushButton::ALightSwitchPushButton() 13 | { 14 | LightIntensity = 3000.0f; 15 | 16 | PointLight = CreateDefaultSubobject(TEXT("Point Light")); 17 | PointLight->Intensity = LightIntensity; 18 | PointLight->bVisible = true; 19 | RootComponent = PointLight; 20 | 21 | LightSphere = CreateDefaultSubobject(TEXT("Light Sphere Component")); 22 | LightSphere->InitSphereRadius(300.0f); 23 | LightSphere->SetCollisionProfileName(TEXT("Trigger")); 24 | LightSphere->SetCollisionResponseToChannel(ECC_Pawn, ECR_Ignore); 25 | LightSphere->SetupAttachment(RootComponent); 26 | } 27 | 28 | void ALightSwitchPushButton::ToggleLight() 29 | { 30 | PointLight->ToggleVisibility(); 31 | } 32 | -------------------------------------------------------------------------------- /LightSwitchPushButton/LightSwitchPushButton.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "LightSwitchPushButton.generated.h" 11 | 12 | class UPointLightComponent; 13 | class USphereComponent; 14 | 15 | UCLASS() 16 | class UNREALCPP_API ALightSwitchPushButton : public AActor 17 | { 18 | GENERATED_BODY() 19 | 20 | public: 21 | // Sets default values for this actor's properties 22 | ALightSwitchPushButton(); 23 | 24 | // declare point light comp 25 | UPROPERTY(VisibleAnywhere, Category = "Light Switch") 26 | class UPointLightComponent* PointLight; 27 | 28 | // declare sphere comp 29 | UPROPERTY(VisibleAnywhere, Category = "Light Switch") 30 | class USphereComponent* LightSphere; 31 | 32 | // declare light intensity variable 33 | UPROPERTY(VisibleAnywhere, Category = "Light Switch") 34 | float LightIntensity; 35 | 36 | // declare ToggleLight function 37 | UFUNCTION(BlueprintCallable, Category = "Light Switch") 38 | void ToggleLight(); 39 | }; 40 | -------------------------------------------------------------------------------- /LightSwitchPushButton/UnrealCPPCharacter.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Character.h" 7 | #include "UnrealCPPCharacter.generated.h" 8 | 9 | class UInputComponent; 10 | class ALightSwitchPushButton; 11 | 12 | UCLASS(config=Game) 13 | class AUnrealCPPCharacter : public ACharacter 14 | { 15 | GENERATED_BODY() 16 | 17 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 18 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 19 | class USkeletalMeshComponent* Mesh1P; 20 | 21 | /** Gun mesh: 1st person view (seen only by self) */ 22 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 23 | class USkeletalMeshComponent* FP_Gun; 24 | 25 | /** Location on gun mesh where projectiles should spawn. */ 26 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 27 | class USceneComponent* FP_MuzzleLocation; 28 | 29 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 30 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 31 | class USkeletalMeshComponent* VR_Gun; 32 | 33 | /** Location on VR gun mesh where projectiles should spawn. */ 34 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 35 | class USceneComponent* VR_MuzzleLocation; 36 | 37 | /** First person camera */ 38 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 39 | class UCameraComponent* FirstPersonCameraComponent; 40 | 41 | /** Motion controller (right hand) */ 42 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 43 | class UMotionControllerComponent* R_MotionController; 44 | 45 | /** Motion controller (left hand) */ 46 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 47 | class UMotionControllerComponent* L_MotionController; 48 | 49 | // create trigger capsule 50 | UPROPERTY(VisibleAnywhere, Category = "Trigger Capsule") 51 | class UCapsuleComponent* TriggerCapsule; 52 | 53 | public: 54 | AUnrealCPPCharacter(); 55 | 56 | protected: 57 | virtual void BeginPlay(); 58 | 59 | public: 60 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 61 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 62 | float BaseTurnRate; 63 | 64 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 65 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 66 | float BaseLookUpRate; 67 | 68 | /** Gun muzzle's offset from the characters location */ 69 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 70 | FVector GunOffset; 71 | 72 | /** Projectile class to spawn */ 73 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 74 | TSubclassOf ProjectileClass; 75 | 76 | /** Sound to play each time we fire */ 77 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 78 | class USoundBase* FireSound; 79 | 80 | /** AnimMontage to play each time we fire */ 81 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 82 | class UAnimMontage* FireAnimation; 83 | 84 | /** Whether to use motion controller location for aiming. */ 85 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 86 | uint32 bUsingMotionControllers : 1; 87 | 88 | // declare overlap begin function 89 | UFUNCTION() 90 | void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); 91 | 92 | // declare overlap end function 93 | UFUNCTION() 94 | void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); 95 | 96 | // declare current light switch 97 | class ALightSwitchPushButton* CurrentLightSwitch; 98 | 99 | protected: 100 | 101 | /** Fires a projectile. */ 102 | void OnFire(); 103 | 104 | // on action 105 | void OnAction(); 106 | 107 | /** Resets HMD orientation and position in VR. */ 108 | void OnResetVR(); 109 | 110 | /** Handles moving forward/backward */ 111 | void MoveForward(float Val); 112 | 113 | /** Handles stafing movement, left and right */ 114 | void MoveRight(float Val); 115 | 116 | /** 117 | * Called via input to turn at a given rate. 118 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 119 | */ 120 | void TurnAtRate(float Rate); 121 | 122 | /** 123 | * Called via input to turn look up/down at a given rate. 124 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 125 | */ 126 | void LookUpAtRate(float Rate); 127 | 128 | struct TouchData 129 | { 130 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 131 | bool bIsPressed; 132 | ETouchIndex::Type FingerIndex; 133 | FVector Location; 134 | bool bMoved; 135 | }; 136 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 137 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 138 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 139 | TouchData TouchItem; 140 | 141 | protected: 142 | // APawn interface 143 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 144 | // End of APawn interface 145 | 146 | /* 147 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 148 | * 149 | * @param InputComponent The input component pointer to bind controls to 150 | * @returns true if touch controls were enabled. 151 | */ 152 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 153 | 154 | public: 155 | /** Returns Mesh1P subobject **/ 156 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 157 | /** Returns FirstPersonCameraComponent subobject **/ 158 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 159 | 160 | }; 161 | -------------------------------------------------------------------------------- /LightSwitchTrigger/LightSwitchTrigger.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "LightSwitchTrigger.h" 8 | #include "Components/PointLightComponent.h" 9 | #include "Components/SphereComponent.h" 10 | #include "DrawDebugHelpers.h" 11 | 12 | // Sets default values 13 | ALightSwitchTrigger::ALightSwitchTrigger() 14 | { 15 | LightIntensity = 3000.0f; 16 | 17 | PointLight = CreateDefaultSubobject(TEXT("Point Light")); 18 | PointLight->Intensity = LightIntensity; 19 | PointLight->bVisible = true; 20 | RootComponent = PointLight; 21 | 22 | LightSphere = CreateDefaultSubobject(TEXT("Light Sphere Component")); 23 | LightSphere->InitSphereRadius(300.0f); 24 | LightSphere->SetCollisionProfileName(TEXT("Trigger")); 25 | LightSphere->SetupAttachment(RootComponent); 26 | 27 | LightSphere->OnComponentBeginOverlap.AddDynamic(this, &ALightSwitchTrigger::OnOverlapBegin); 28 | LightSphere->OnComponentEndOverlap.AddDynamic(this, &ALightSwitchTrigger::OnOverlapEnd); 29 | } 30 | 31 | // Called when the game starts or when spawned 32 | void ALightSwitchTrigger::BeginPlay() 33 | { 34 | Super::BeginPlay(); 35 | 36 | DrawDebugSphere(GetWorld(), GetActorLocation(), 300.f, 50, FColor::Green, true, 999, 0, 2); 37 | 38 | } 39 | 40 | void ALightSwitchTrigger::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) 41 | { 42 | if (OtherActor && (OtherActor != this) && OtherComp) 43 | { 44 | ToggleLight(); 45 | } 46 | } 47 | 48 | void ALightSwitchTrigger::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) 49 | { 50 | if (OtherActor && (OtherActor != this) && OtherComp) 51 | { 52 | ToggleLight(); 53 | } 54 | } 55 | 56 | void ALightSwitchTrigger::ToggleLight() 57 | { 58 | PointLight->ToggleVisibility(); 59 | } -------------------------------------------------------------------------------- /LightSwitchTrigger/LightSwitchTrigger.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "LightSwitchTrigger.generated.h" 11 | 12 | class UPointLightComponent; 13 | class USphereComponent; 14 | 15 | UCLASS() 16 | class UNREALCPP_API ALightSwitchTrigger : public AActor 17 | { 18 | GENERATED_BODY() 19 | 20 | public: 21 | // Sets default values for this actor's properties 22 | ALightSwitchTrigger(); 23 | 24 | protected: 25 | // Called when the game starts or when spawned 26 | virtual void BeginPlay() override; 27 | 28 | public: 29 | // declare point light comp 30 | UPROPERTY(VisibleAnywhere, Category = "Light Switch") 31 | class UPointLightComponent* PointLight; 32 | 33 | // declare sphere comp 34 | UPROPERTY(VisibleAnywhere, Category = "Light Switch") 35 | class USphereComponent* LightSphere; 36 | 37 | // declare light intensity variable 38 | UPROPERTY(VisibleAnywhere, Category = "Light Switch") 39 | float LightIntensity; 40 | 41 | // declare overlap begin function 42 | UFUNCTION() 43 | void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); 44 | 45 | // declare overlap end function 46 | UFUNCTION() 47 | void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); 48 | 49 | // declare ToggleLight function 50 | UFUNCTION() 51 | void ToggleLight(); 52 | 53 | }; 54 | -------------------------------------------------------------------------------- /LineTraceOnTick/UnrealCPPCharacter.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Character.h" 7 | #include "UnrealCPPCharacter.generated.h" 8 | 9 | class UInputComponent; 10 | 11 | UCLASS(config=Game) 12 | class AUnrealCPPCharacter : public ACharacter 13 | { 14 | GENERATED_BODY() 15 | 16 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 17 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 18 | class USkeletalMeshComponent* Mesh1P; 19 | 20 | /** Gun mesh: 1st person view (seen only by self) */ 21 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 22 | class USkeletalMeshComponent* FP_Gun; 23 | 24 | /** Location on gun mesh where projectiles should spawn. */ 25 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 26 | class USceneComponent* FP_MuzzleLocation; 27 | 28 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 29 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 30 | class USkeletalMeshComponent* VR_Gun; 31 | 32 | /** Location on VR gun mesh where projectiles should spawn. */ 33 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 34 | class USceneComponent* VR_MuzzleLocation; 35 | 36 | /** First person camera */ 37 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 38 | class UCameraComponent* FirstPersonCameraComponent; 39 | 40 | /** Motion controller (right hand) */ 41 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 42 | class UMotionControllerComponent* R_MotionController; 43 | 44 | /** Motion controller (left hand) */ 45 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 46 | class UMotionControllerComponent* L_MotionController; 47 | 48 | public: 49 | AUnrealCPPCharacter(); 50 | 51 | protected: 52 | virtual void BeginPlay(); 53 | 54 | public: 55 | // Called every frame 56 | virtual void Tick(float DeltaTime) override; 57 | 58 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 59 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 60 | float BaseTurnRate; 61 | 62 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 63 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 64 | float BaseLookUpRate; 65 | 66 | /** Gun muzzle's offset from the characters location */ 67 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 68 | FVector GunOffset; 69 | 70 | /** Projectile class to spawn */ 71 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 72 | TSubclassOf ProjectileClass; 73 | 74 | /** Sound to play each time we fire */ 75 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 76 | class USoundBase* FireSound; 77 | 78 | /** AnimMontage to play each time we fire */ 79 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 80 | class UAnimMontage* FireAnimation; 81 | 82 | /** Whether to use motion controller location for aiming. */ 83 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 84 | uint32 bUsingMotionControllers : 1; 85 | 86 | protected: 87 | 88 | /** Fires a projectile. */ 89 | void OnFire(); 90 | 91 | /** Resets HMD orientation and position in VR. */ 92 | void OnResetVR(); 93 | 94 | /** Handles moving forward/backward */ 95 | void MoveForward(float Val); 96 | 97 | /** Handles stafing movement, left and right */ 98 | void MoveRight(float Val); 99 | 100 | /** 101 | * Called via input to turn at a given rate. 102 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 103 | */ 104 | void TurnAtRate(float Rate); 105 | 106 | /** 107 | * Called via input to turn look up/down at a given rate. 108 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 109 | */ 110 | void LookUpAtRate(float Rate); 111 | 112 | struct TouchData 113 | { 114 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 115 | bool bIsPressed; 116 | ETouchIndex::Type FingerIndex; 117 | FVector Location; 118 | bool bMoved; 119 | }; 120 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 121 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 122 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 123 | TouchData TouchItem; 124 | 125 | protected: 126 | // APawn interface 127 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 128 | // End of APawn interface 129 | 130 | /* 131 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 132 | * 133 | * @param InputComponent The input component pointer to bind controls to 134 | * @returns true if touch controls were enabled. 135 | */ 136 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 137 | 138 | public: 139 | /** Returns Mesh1P subobject **/ 140 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 141 | /** Returns FirstPersonCameraComponent subobject **/ 142 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 143 | 144 | }; 145 | -------------------------------------------------------------------------------- /MyDrawDebugHelpers/MyDrawDebugHelpers.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MyDrawDebugHelpers.h" 8 | // include draw debu helpers header file 9 | #include "DrawDebugHelpers.h" 10 | 11 | // Sets default values 12 | AMyDrawDebugHelpers::AMyDrawDebugHelpers() 13 | { 14 | // init variables with values 15 | LocationOne = FVector(0,0,600); 16 | LocationTwo = FVector(0,-600,600); 17 | LocationThree = FVector(0,600,600); 18 | LocationFour = FVector(-300,0,600); 19 | LocationFive = FVector(-400,-600,600); 20 | 21 | MyBox = FBox(FVector(0,0,0), FVector(200,200,200)); 22 | } 23 | 24 | // Called when the game starts or when spawned 25 | void AMyDrawDebugHelpers::BeginPlay() 26 | { 27 | Super::BeginPlay(); 28 | 29 | DrawDebugPoint(GetWorld(), LocationOne, 200, FColor(52,220,239), true, 999); 30 | 31 | DrawDebugSphere(GetWorld(), LocationTwo, 200, 26, FColor(181,0,0), true, 999, 0, 2); 32 | 33 | DrawDebugCircle(GetWorld(), CircleMatrix, 200, 50, FColor(0,104,167), true, 999, 0, 10); 34 | 35 | DrawDebugCircle(GetWorld(), LocationFour, 200, 50, FColor(0,0,0), true, 999, 0, 10); 36 | 37 | DrawDebugSolidBox(GetWorld(), MyBox, FColor(20, 100, 240), MyTransform, true, 999); 38 | 39 | DrawDebugBox(GetWorld(), LocationFive, FVector(100,100,100), FColor::Purple, true, 999, 0, 10); 40 | 41 | DrawDebugLine(GetWorld(), LocationTwo, LocationThree, FColor::Emerald, true, 999, 0, 10); 42 | 43 | DrawDebugDirectionalArrow(GetWorld(), FVector(-300, 600, 600), FVector(-300, -600, 600), 120.f, FColor::Magenta, true, 999, 0, 5.f); 44 | 45 | DrawDebugCrosshairs(GetWorld(), FVector(0,0,1000), FRotator(0,0,0), 500.f, FColor::White, true, 999, 0); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /MyDrawDebugHelpers/MyDrawDebugHelpers.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "MyDrawDebugHelpers.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AMyDrawDebugHelpers : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | AMyDrawDebugHelpers(); 20 | 21 | protected: 22 | // Called when the game starts or when spawned 23 | virtual void BeginPlay() override; 24 | 25 | public: 26 | 27 | // declare location variables 28 | UPROPERTY(EditAnywhere, Category = "Locations") 29 | FVector LocationOne; 30 | 31 | UPROPERTY(EditAnywhere, Category = "Locations") 32 | FVector LocationTwo; 33 | 34 | UPROPERTY(EditAnywhere, Category = "Locations") 35 | FVector LocationThree; 36 | 37 | UPROPERTY(EditAnywhere, Category = "Locations") 38 | FVector LocationFour; 39 | 40 | UPROPERTY(EditAnywhere, Category = "Locations") 41 | FVector LocationFive; 42 | 43 | UPROPERTY(EditAnywhere, Category = "Locations") 44 | FMatrix CircleMatrix; 45 | 46 | UPROPERTY(EditAnywhere, Category = "Locations") 47 | FBox MyBox; 48 | 49 | UPROPERTY(EditAnywhere, Category = "Locations") 50 | FTransform MyTransform; 51 | 52 | 53 | 54 | }; 55 | -------------------------------------------------------------------------------- /MyInterface/MyInterface.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MyInterface.h" 8 | 9 | 10 | // Add default functionality here for any IMyInterface functions that are not pure virtual. 11 | void IMyInterface::SaySomething() 12 | { 13 | UE_LOG(LogTemp, Warning, TEXT("Hi %s"), *Name); 14 | } 15 | 16 | void IMyInterface::ReactToTriggerBegin() 17 | { 18 | UE_LOG(LogTemp, Warning, TEXT("Enter the Trigger")); 19 | } 20 | 21 | void IMyInterface::ReactToTriggerEnd() 22 | { 23 | UE_LOG(LogTemp, Warning, TEXT("Exit the Trigger")); 24 | } -------------------------------------------------------------------------------- /MyInterface/MyInterface.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "UObject/Interface.h" 10 | #include "MyInterface.generated.h" 11 | 12 | // This class does not need to be modified. 13 | UINTERFACE(MinimalAPI) 14 | class UMyInterface : public UInterface 15 | { 16 | GENERATED_BODY() 17 | }; 18 | 19 | 20 | class UNREALCPP_API IMyInterface 21 | { 22 | GENERATED_BODY() 23 | 24 | // Add interface functions to this class. This is the class that will be inherited to implement this interface. 25 | public: 26 | /** React to a trigger volume activating this object. Return true if the reaction succeeds. */ 27 | UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category="Trigger Reaction") 28 | bool ReactToTrigger() const; 29 | 30 | virtual void SaySomething(); 31 | 32 | virtual void ReactToTriggerBegin(); 33 | virtual void ReactToTriggerEnd(); 34 | 35 | FString Name; 36 | }; 37 | -------------------------------------------------------------------------------- /MyInterface/MyInterfaceActor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MyInterfaceActor.h" 8 | #include "Components/TimelineComponent.h" 9 | 10 | // Sets default values 11 | AMyInterfaceActor::AMyInterfaceActor() 12 | { 13 | MyTimeline = CreateDefaultSubobject(TEXT("My Timeline")); 14 | 15 | Name = FString(TEXT("Harry")); 16 | } 17 | 18 | void AMyInterfaceActor::MoveToLocation(float value) 19 | { 20 | SetActorLocation(FMath::Lerp(Start, End, value)); 21 | } 22 | 23 | void AMyInterfaceActor::ReactToTriggerBegin() 24 | { 25 | IMyInterface::ReactToTriggerBegin(); 26 | 27 | if (MyCurve) 28 | { 29 | FOnTimelineFloat TimelineCallback; 30 | TimelineCallback.BindUFunction(this, FName("MoveToLocation")); 31 | MyTimeline->AddInterpFloat(MyCurve, TimelineCallback); 32 | MyTimeline->SetLooping(false); 33 | Start = GetActorLocation(); 34 | End = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation(); 35 | MyTimeline->PlayFromStart(); 36 | } 37 | } 38 | 39 | void AMyInterfaceActor::ReactToTriggerEnd() 40 | { 41 | IMyInterface::ReactToTriggerEnd(); 42 | 43 | if (MyCurve) 44 | { 45 | FOnTimelineFloat TimelineCallback; 46 | TimelineCallback.BindUFunction(this, FName("MoveToLocation")); 47 | MyTimeline->AddInterpFloat(MyCurve, TimelineCallback); 48 | MyTimeline->SetLooping(false); 49 | End = Start; 50 | Start = GetActorLocation(); 51 | MyTimeline->PlayFromStart(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /MyInterface/MyInterfaceActor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "MyInterface.h" 10 | #include "GameFramework/Actor.h" 11 | #include "MyInterfaceActor.generated.h" 12 | 13 | class UTimelineComponent; 14 | 15 | UCLASS() 16 | class UNREALCPP_API AMyInterfaceActor : public AActor, public IMyInterface 17 | { 18 | GENERATED_BODY() 19 | 20 | public: 21 | // Sets default values for this actor's properties 22 | AMyInterfaceActor(); 23 | 24 | // virtual void SaySomething() override; 25 | virtual void ReactToTriggerBegin() override; 26 | virtual void ReactToTriggerEnd() override; 27 | 28 | UPROPERTY(EditAnywhere) 29 | UCurveFloat* MyCurve; 30 | 31 | UPROPERTY(EditAnywhere) 32 | UTimelineComponent* MyTimeline; 33 | 34 | UPROPERTY() 35 | FVector Start; 36 | 37 | UPROPERTY() 38 | FVector End; 39 | 40 | UFUNCTION() 41 | void MoveToLocation(float value); 42 | }; 43 | -------------------------------------------------------------------------------- /MyInterface/MyInterfaceActorTwo.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MyInterfaceActorTwo.h" 8 | #include "Components/TimelineComponent.h" 9 | 10 | // Sets default values 11 | AMyInterfaceActorTwo::AMyInterfaceActorTwo() 12 | { 13 | MyTimeline = CreateDefaultSubobject(TEXT("My Timeline")); 14 | 15 | Name = FString(TEXT("Eddy")); 16 | } 17 | 18 | void AMyInterfaceActorTwo::MoveToLocation(float value) 19 | { 20 | SetActorLocation(FMath::Lerp(Start, End, value)); 21 | } 22 | 23 | void AMyInterfaceActorTwo::ReactToTriggerBegin() 24 | { 25 | IMyInterface::ReactToTriggerBegin(); 26 | 27 | if (MyCurve) 28 | { 29 | FOnTimelineFloat TimelineCallback; 30 | TimelineCallback.BindUFunction(this, FName("MoveToLocation")); 31 | MyTimeline->AddInterpFloat(MyCurve, TimelineCallback); 32 | MyTimeline->SetLooping(false); 33 | Start = GetActorLocation(); 34 | End = Start*5; 35 | MyTimeline->PlayFromStart(); 36 | } 37 | } 38 | 39 | void AMyInterfaceActorTwo::ReactToTriggerEnd() 40 | { 41 | IMyInterface::ReactToTriggerEnd(); 42 | 43 | if (MyCurve) 44 | { 45 | FOnTimelineFloat TimelineCallback; 46 | TimelineCallback.BindUFunction(this, FName("MoveToLocation")); 47 | MyTimeline->AddInterpFloat(MyCurve, TimelineCallback); 48 | MyTimeline->SetLooping(false); 49 | End = Start; 50 | Start = GetActorLocation(); 51 | MyTimeline->PlayFromStart(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /MyInterface/MyInterfaceActorTwo.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "MyInterface.h" 10 | #include "GameFramework/Actor.h" 11 | #include "MyInterfaceActorTwo.generated.h" 12 | 13 | class UTimelineComponent; 14 | 15 | UCLASS() 16 | class UNREALCPP_API AMyInterfaceActorTwo : public AActor, public IMyInterface 17 | { 18 | GENERATED_BODY() 19 | 20 | public: 21 | // Sets default values for this actor's properties 22 | AMyInterfaceActorTwo(); 23 | 24 | // virtual void SaySomething() override; 25 | virtual void ReactToTriggerBegin() override; 26 | virtual void ReactToTriggerEnd() override; 27 | 28 | UPROPERTY(EditAnywhere) 29 | UCurveFloat* MyCurve; 30 | 31 | UPROPERTY(EditAnywhere) 32 | UTimelineComponent* MyTimeline; 33 | 34 | UPROPERTY() 35 | FVector Start; 36 | 37 | UPROPERTY() 38 | FVector End; 39 | 40 | UFUNCTION() 41 | void MoveToLocation(float value); 42 | }; 43 | -------------------------------------------------------------------------------- /MyInterface/MyInterfaceTriggerVolume.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MyInterfaceTriggerVolume.h" 8 | #include "Kismet/GameplayStatics.h" 9 | #include "MyInterface.h" 10 | 11 | AMyInterfaceTriggerVolume::AMyInterfaceTriggerVolume() 12 | { 13 | OnActorBeginOverlap.AddDynamic(this, &AMyInterfaceTriggerVolume::OnOverlapBegin); 14 | OnActorEndOverlap.AddDynamic(this, &AMyInterfaceTriggerVolume::OnOverlapEnd); 15 | } 16 | 17 | void AMyInterfaceTriggerVolume::OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor) 18 | { 19 | // check if Actors do not equal nullptr and that 20 | if (OtherActor && OtherActor != this && !Cast(OtherActor)) 21 | { 22 | TArray MyInterfaceActors; 23 | UGameplayStatics::GetAllActorsWithInterface(GetWorld(), UMyInterface::StaticClass(), MyInterfaceActors); 24 | 25 | if(MyInterfaceActors.Num() > 0) 26 | { 27 | for (auto& Act : MyInterfaceActors) 28 | { 29 | IMyInterface* InterfaceActor = Cast(Act); 30 | if(InterfaceActor) 31 | { 32 | InterfaceActor->SaySomething(); 33 | InterfaceActor->ReactToTriggerBegin(); 34 | } 35 | } 36 | } 37 | } 38 | } 39 | 40 | void AMyInterfaceTriggerVolume::OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor) 41 | { 42 | if (OtherActor && OtherActor != this && !Cast(OtherActor)) 43 | { 44 | TArray MyInterfaceActors; 45 | UGameplayStatics::GetAllActorsWithInterface(GetWorld(), UMyInterface::StaticClass(), MyInterfaceActors); 46 | 47 | if(MyInterfaceActors.Num() > 0) 48 | { 49 | for (auto& Act : MyInterfaceActors) 50 | { 51 | IMyInterface* InterfaceActor = Cast(Act); 52 | if(InterfaceActor) 53 | { 54 | InterfaceActor->ReactToTriggerEnd(); 55 | } 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /MyInterface/MyInterfaceTriggerVolume.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "Engine/TriggerVolume.h" 10 | #include "MyInterfaceTriggerVolume.generated.h" 11 | 12 | 13 | UCLASS() 14 | class UNREALCPP_API AMyInterfaceTriggerVolume : public ATriggerVolume 15 | { 16 | GENERATED_BODY() 17 | 18 | public: 19 | AMyInterfaceTriggerVolume(); 20 | 21 | // overlap begin function 22 | UFUNCTION() 23 | void OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor); 24 | 25 | // overlap end function 26 | UFUNCTION() 27 | void OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor); 28 | }; 29 | -------------------------------------------------------------------------------- /MyPawn/MyPawn.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | // helpful link 8 | // https://docs.unrealengine.com/latest/INT/Programming/Tutorials/PlayerInput/index.html 9 | 10 | #include "MyPawn.h" 11 | #include "Camera/CameraComponent.h" 12 | #include "Components/InputComponent.h" 13 | #include "Components/StaticMeshComponent.h" 14 | 15 | // Sets default values 16 | AMyPawn::AMyPawn() 17 | { 18 | // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. 19 | PrimaryActorTick.bCanEverTick = true; 20 | 21 | // Set this pawn to be controlled by the lowest-numbered player 22 | AutoPossessPlayer = EAutoReceiveInput::Player0; 23 | 24 | // Create a dummy root component we can attach things to. 25 | RootComponent = CreateDefaultSubobject(TEXT("RootComponent")); 26 | // Create a camera and a visible object 27 | UCameraComponent* OurCamera = CreateDefaultSubobject(TEXT("OurCamera")); 28 | OurVisibleComponent = CreateDefaultSubobject(TEXT("OurVisibleComponent")); 29 | // Attach our camera and visible object to our root component. Offset and rotate the camera. 30 | OurCamera->SetupAttachment(RootComponent); 31 | OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f)); 32 | OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f)); 33 | OurVisibleComponent->SetupAttachment(RootComponent); 34 | 35 | } 36 | 37 | // Called every frame 38 | void AMyPawn::Tick(float DeltaTime) 39 | { 40 | Super::Tick(DeltaTime); 41 | 42 | 43 | // Handle growing and shrinking based on our "Grow" action 44 | { 45 | float CurrentScale = OurVisibleComponent->GetComponentScale().X; 46 | if (bGrowing) 47 | { 48 | // Grow to double size over the course of one second 49 | CurrentScale += DeltaTime; 50 | } 51 | else 52 | { 53 | // Shrink half as fast as we grow 54 | CurrentScale -= (DeltaTime * 0.5f); 55 | } 56 | // Make sure we never drop below our starting size, or increase past double size. 57 | CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f); 58 | OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale)); 59 | } 60 | 61 | // Handle movement based on our "MoveX" and "MoveY" axes 62 | { 63 | if (!CurrentVelocity.IsZero()) 64 | { 65 | FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime); 66 | SetActorLocation(NewLocation); 67 | } 68 | } 69 | 70 | } 71 | 72 | // Called to bind functionality to input 73 | void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) 74 | { 75 | Super::SetupPlayerInputComponent(InputComponent); 76 | 77 | // Respond when our "Grow" key is pressed or released. 78 | InputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::StartGrowing); 79 | InputComponent->BindAction("Grow", IE_Released, this, &AMyPawn::StopGrowing); 80 | 81 | // Respond every frame to the values of our two movement axes, "MoveX" and "MoveY". 82 | InputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis); 83 | InputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis); 84 | } 85 | 86 | void AMyPawn::Move_XAxis(float AxisValue) 87 | { 88 | // Move at 100 units per second forward or backward 89 | CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f; 90 | } 91 | 92 | void AMyPawn::Move_YAxis(float AxisValue) 93 | { 94 | // Move at 100 units per second right or left 95 | CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f; 96 | } 97 | 98 | void AMyPawn::StartGrowing() 99 | { 100 | bGrowing = true; 101 | } 102 | 103 | void AMyPawn::StopGrowing() 104 | { 105 | bGrowing = false; 106 | } 107 | -------------------------------------------------------------------------------- /MyPawn/MyPawn.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Pawn.h" 10 | #include "MyPawn.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AMyPawn : public APawn 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this pawn's properties 19 | AMyPawn(); 20 | 21 | // Called every frame 22 | virtual void Tick(float DeltaTime) override; 23 | 24 | // Called to bind functionality to input 25 | virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; 26 | 27 | UPROPERTY(EditAnywhere) 28 | USceneComponent* OurVisibleComponent; 29 | 30 | // Input functions 31 | void Move_XAxis(float AxisValue); 32 | void Move_YAxis(float AxisValue); 33 | void StartGrowing(); 34 | void StopGrowing(); 35 | 36 | // Input variables 37 | FVector CurrentVelocity; 38 | bool bGrowing; 39 | 40 | }; 41 | -------------------------------------------------------------------------------- /MyStaticMeshActor/MyStaticMeshActor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MyStaticMeshActor.h" 8 | -------------------------------------------------------------------------------- /MyStaticMeshActor/MyStaticMeshActor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "Engine/StaticMeshActor.h" 10 | #include "MyStaticMeshActor.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AMyStaticMeshActor : public AStaticMeshActor 14 | { 15 | GENERATED_BODY() 16 | }; 17 | -------------------------------------------------------------------------------- /MySweepActor/MySweepActor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MySweepActor.h" 8 | #include "DrawDebugHelpers.h" 9 | 10 | // Called when the game starts or when spawned 11 | void AMySweepActor::BeginPlay() 12 | { 13 | Super::BeginPlay(); 14 | 15 | // create tarray for hit results 16 | TArray OutHits; 17 | 18 | // start and end locations 19 | FVector SweepStart = GetActorLocation(); 20 | FVector SweepEnd = GetActorLocation(); 21 | 22 | // create a collision sphere 23 | FCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f); 24 | 25 | // draw collision sphere 26 | DrawDebugSphere(GetWorld(), GetActorLocation(), MyColSphere.GetSphereRadius(), 50, FColor::Purple, true, 999); 27 | 28 | // check if something got hit in the sweep 29 | bool isHit = GetWorld()->SweepMultiByChannel(OutHits, SweepStart, SweepEnd, FQuat::Identity, ECC_WorldStatic, MyColSphere); 30 | 31 | if (isHit) 32 | { 33 | // loop through TArray 34 | for (auto& Hit : OutHits) 35 | { 36 | if (GEngine) 37 | { 38 | // screen log information on what was hit 39 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Hit Result: %s"), *Hit.Actor->GetName())); 40 | // uncommnet to see more info on sweeped actor 41 | // GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("All Hit Information: %s"), *Hit.ToString())); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /MySweepActor/MySweepActor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "MySweepActor.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AMySweepActor : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | protected: 18 | // Called when the game starts or when spawned 19 | virtual void BeginPlay() override; 20 | }; 21 | -------------------------------------------------------------------------------- /OnComponentHit/OnComponentHit.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "OnComponentHit.h" 8 | #include "Components/BoxComponent.h" 9 | 10 | // Sets default values 11 | AOnComponentHit::AOnComponentHit() 12 | { 13 | // Use a sphere as a simple collision representation 14 | MyComp = CreateDefaultSubobject(TEXT("BoxComp")); 15 | MyComp->SetSimulatePhysics(true); 16 | MyComp->SetNotifyRigidBodyCollision(true); 17 | MyComp->BodyInstance.SetCollisionProfileName("BlockAllDynamic"); 18 | MyComp->OnComponentHit.AddDynamic(this, &AOnComponentHit::OnCompHit); 19 | 20 | // Set as root component 21 | RootComponent = MyComp; 22 | } 23 | 24 | void AOnComponentHit::OnCompHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) 25 | { 26 | if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL)) 27 | { 28 | if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("I Hit: %s"), *OtherActor->GetName())); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /OnComponentHit/OnComponentHit.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "OnComponentHit.generated.h" 11 | 12 | class UBoxComponent; 13 | 14 | UCLASS() 15 | class UNREALCPP_API AOnComponentHit : public AActor 16 | { 17 | GENERATED_BODY() 18 | 19 | public: 20 | // Sets default values for this actor's properties 21 | AOnComponentHit(); 22 | 23 | UPROPERTY(VisibleAnywhere) 24 | class UBoxComponent* MyComp; 25 | 26 | UFUNCTION() 27 | void OnCompHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit); 28 | }; 29 | -------------------------------------------------------------------------------- /OpenDoorTimelineCurve/OpenDoorTimelineCurve.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | // helpful links 8 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/FTimeline/ 9 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UTimelineComponent/index.html 10 | // https://wiki.unrealengine.com/Timeline_in_c%2B%2B#How_to_use_Timeline_in_C.2B.2B 11 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Curves/UCurveFloat/GetFloatValue/index.html 12 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Curves/UCurveFloat/ 13 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/OnComponentBeginOverlap/ 14 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/OnComponentEndOverlap/ 15 | // https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Vector/UnrotateVector/ 16 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Kismet/UKismetMathLibrary/LessLess_VectorRotator/index.html 17 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Kismet/UKismetMathLibrary/index.html 18 | 19 | #include "OpenDoorTimelineCurve.h" 20 | #include "Kismet/KismetMathLibrary.h" 21 | #include "Kismet/GameplayStatics.h" 22 | #include "Components/TimelineComponent.h" 23 | 24 | // Sets default values 25 | AOpenDoorTimelineCurve::AOpenDoorTimelineCurve() 26 | { 27 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 28 | PrimaryActorTick.bCanEverTick = true; 29 | 30 | Open = false; 31 | ReadyState = true; 32 | 33 | DoorFrame = CreateDefaultSubobject(TEXT("DoorFrame")); 34 | RootComponent = DoorFrame; 35 | 36 | Door = CreateDefaultSubobject(TEXT("My Mesh")); 37 | Door->SetupAttachment(RootComponent); 38 | 39 | } 40 | 41 | // Called when the game starts or when spawned 42 | void AOpenDoorTimelineCurve::BeginPlay() 43 | { 44 | Super::BeginPlay(); 45 | 46 | RotateValue = 1.0f; 47 | 48 | if (DoorCurve) 49 | { 50 | FOnTimelineFloat TimelineCallback; 51 | FOnTimelineEventStatic TimelineFinishedCallback; 52 | 53 | TimelineCallback.BindUFunction(this, FName("ControlDoor")); 54 | TimelineFinishedCallback.BindUFunction(this, FName("SetState")); 55 | 56 | MyTimeline = NewObject(this, FName("DoorAnimation")); 57 | MyTimeline->AddInterpFloat(DoorCurve, TimelineCallback); 58 | MyTimeline->SetTimelineFinishedFunc(TimelineFinishedCallback); 59 | MyTimeline->RegisterComponent(); 60 | } 61 | 62 | } 63 | 64 | // Called every frame 65 | void AOpenDoorTimelineCurve::Tick(float DeltaTime) 66 | { 67 | Super::Tick(DeltaTime); 68 | 69 | if (MyTimeline != NULL) 70 | { 71 | MyTimeline->TickComponent(DeltaTime, ELevelTick::LEVELTICK_TimeOnly, NULL); 72 | } 73 | } 74 | 75 | void AOpenDoorTimelineCurve::ControlDoor() 76 | { 77 | TimelineValue = MyTimeline->GetPlaybackPosition(); 78 | CurveFloatValue = RotateValue*DoorCurve->GetFloatValue(TimelineValue); 79 | 80 | FQuat NewRotation = FQuat(FRotator(0.f, CurveFloatValue, 0.f)); 81 | 82 | Door->SetRelativeRotation(NewRotation); 83 | } 84 | 85 | void AOpenDoorTimelineCurve::SetState() 86 | { 87 | ReadyState = true; 88 | } 89 | 90 | void AOpenDoorTimelineCurve::ToggleDoor() 91 | { 92 | if(ReadyState) 93 | { 94 | Open = !Open; 95 | 96 | // alternative way to get pawn position 97 | // GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation() 98 | APawn* OurPawn = UGameplayStatics::GetPlayerPawn(this, 0); 99 | FVector PawnLocation = OurPawn->GetActorLocation(); 100 | FVector Direction = GetActorLocation() - PawnLocation; 101 | Direction = UKismetMathLibrary::LessLess_VectorRotator(Direction, GetActorRotation()); 102 | 103 | if(Open) 104 | { 105 | if(Direction.X > 0.0f) 106 | { 107 | RotateValue = 1.0f; 108 | } 109 | else 110 | { 111 | RotateValue = -1.0f; 112 | } 113 | 114 | ReadyState = false; 115 | if (MyTimeline != NULL) 116 | { 117 | MyTimeline->PlayFromStart(); 118 | } 119 | } 120 | else 121 | { 122 | ReadyState = false; 123 | if (MyTimeline != NULL) 124 | { 125 | MyTimeline->Reverse(); 126 | } 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /OpenDoorTimelineCurve/OpenDoorTimelineCurve.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "OpenDoorTimelineCurve.generated.h" 11 | 12 | class UBoxComponent; 13 | class UTimelineComponent; 14 | 15 | UCLASS() 16 | class UNREALCPP_API AOpenDoorTimelineCurve : public AActor 17 | { 18 | GENERATED_BODY() 19 | 20 | public: 21 | // Sets default values for this actor's properties 22 | AOpenDoorTimelineCurve(); 23 | 24 | protected: 25 | // Called when the game starts or when spawned 26 | virtual void BeginPlay() override; 27 | 28 | public: 29 | // Called every frame 30 | virtual void Tick(float DeltaTime) override; 31 | 32 | UPROPERTY(EditAnywhere) 33 | UStaticMeshComponent* Door; 34 | 35 | UPROPERTY(EditAnywhere) 36 | UStaticMeshComponent* DoorFrame; 37 | 38 | UPROPERTY(EditAnywhere) 39 | UCurveFloat* DoorCurve; 40 | 41 | UFUNCTION() 42 | void ControlDoor(); 43 | 44 | UFUNCTION() 45 | void ToggleDoor(); 46 | 47 | UFUNCTION() 48 | void SetState(); 49 | 50 | bool Open; 51 | bool ReadyState; 52 | float RotateValue; 53 | float CurveFloatValue; 54 | float TimelineValue; 55 | UTimelineComponent* MyTimeline; 56 | }; 57 | -------------------------------------------------------------------------------- /OpenDoorTimelineCurve/UnrealCPPCharacter.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Character.h" 7 | #include "UnrealCPPCharacter.generated.h" 8 | 9 | class UInputComponent; 10 | class UUserWidget; 11 | class AOpenDoorTimelineCurve; 12 | 13 | UCLASS(config=Game) 14 | class AUnrealCPPCharacter : public ACharacter 15 | { 16 | GENERATED_BODY() 17 | 18 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 19 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 20 | class USkeletalMeshComponent* Mesh1P; 21 | 22 | /** Gun mesh: 1st person view (seen only by self) */ 23 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 24 | class USkeletalMeshComponent* FP_Gun; 25 | 26 | /** Location on gun mesh where projectiles should spawn. */ 27 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 28 | class USceneComponent* FP_MuzzleLocation; 29 | 30 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 31 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 32 | class USkeletalMeshComponent* VR_Gun; 33 | 34 | /** Location on VR gun mesh where projectiles should spawn. */ 35 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 36 | class USceneComponent* VR_MuzzleLocation; 37 | 38 | /** First person camera */ 39 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 40 | class UCameraComponent* FirstPersonCameraComponent; 41 | 42 | /** Motion controller (right hand) */ 43 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 44 | class UMotionControllerComponent* R_MotionController; 45 | 46 | /** Motion controller (left hand) */ 47 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 48 | class UMotionControllerComponent* L_MotionController; 49 | 50 | public: 51 | AUnrealCPPCharacter(); 52 | 53 | protected: 54 | virtual void BeginPlay(); 55 | 56 | virtual void Tick(float DeltaSeconds) override; 57 | 58 | public: 59 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 60 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 61 | float BaseTurnRate; 62 | 63 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 64 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 65 | float BaseLookUpRate; 66 | 67 | /** Gun muzzle's offset from the characters location */ 68 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 69 | FVector GunOffset; 70 | 71 | /** Projectile class to spawn */ 72 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 73 | TSubclassOf ProjectileClass; 74 | 75 | /** Sound to play each time we fire */ 76 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 77 | class USoundBase* FireSound; 78 | 79 | /** AnimMontage to play each time we fire */ 80 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 81 | class UAnimMontage* FireAnimation; 82 | 83 | /** Whether to use motion controller location for aiming. */ 84 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 85 | uint32 bUsingMotionControllers : 1; 86 | 87 | UPROPERTY(EditAnywhere) 88 | AOpenDoorTimelineCurve* CurrentDoor; 89 | 90 | // Reference UMG Asset in the Editor 91 | UPROPERTY(EditAnywhere) 92 | TSubclassOf HelpWidgetClass; 93 | 94 | UUserWidget* InfoWidget; 95 | 96 | protected: 97 | 98 | /** Fires a projectile. */ 99 | void OnFire(); 100 | 101 | /** Action Function */ 102 | void OnAction(); 103 | 104 | /** Resets HMD orientation and position in VR. */ 105 | void OnResetVR(); 106 | 107 | /** Handles moving forward/backward */ 108 | void MoveForward(float Val); 109 | 110 | /** Handles stafing movement, left and right */ 111 | void MoveRight(float Val); 112 | 113 | /** 114 | * Called via input to turn at a given rate. 115 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 116 | */ 117 | void TurnAtRate(float Rate); 118 | 119 | /** 120 | * Called via input to turn look up/down at a given rate. 121 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 122 | */ 123 | void LookUpAtRate(float Rate); 124 | 125 | struct TouchData 126 | { 127 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 128 | bool bIsPressed; 129 | ETouchIndex::Type FingerIndex; 130 | FVector Location; 131 | bool bMoved; 132 | }; 133 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 134 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 135 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 136 | TouchData TouchItem; 137 | 138 | protected: 139 | // APawn interface 140 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 141 | // End of APawn interface 142 | 143 | /* 144 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 145 | * 146 | * @param InputComponent The input component pointer to bind controls to 147 | * @returns true if touch controls were enabled. 148 | */ 149 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 150 | 151 | public: 152 | /** Returns Mesh1P subobject **/ 153 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 154 | /** Returns FirstPersonCameraComponent subobject **/ 155 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 156 | 157 | }; 158 | -------------------------------------------------------------------------------- /OpenDoorTimelineCurve/UnrealCpp.Build.cs.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | using UnrealBuildTool; 4 | 5 | public class UnrealCPP : ModuleRules 6 | { 7 | public UnrealCPP(ReadOnlyTargetRules Target) : base(Target) 8 | { 9 | PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; 10 | 11 | PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "UMG", "Slate", "SlateCore" }); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /OpenDoorTimelineOverlap/OpenDoorTimelineOverlap.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | // helpful links 8 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/FTimeline/ 9 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UTimelineComponent/index.html 10 | // https://wiki.unrealengine.com/Timeline_in_c%2B%2B#How_to_use_Timeline_in_C.2B.2B 11 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Curves/UCurveFloat/GetFloatValue/index.html 12 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Curves/UCurveFloat/ 13 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/OnComponentBeginOverlap/ 14 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/OnComponentEndOverlap/ 15 | // https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Vector/UnrotateVector/ 16 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Kismet/UKismetMathLibrary/LessLess_VectorRotator/index.html 17 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Kismet/UKismetMathLibrary/index.html 18 | 19 | #include "OpenDoorTimelineOverlap.h" 20 | #include "Components/TimelineComponent.h" 21 | #include "Components/BoxComponent.h" 22 | #include "Kismet/KismetMathLibrary.h" 23 | 24 | // Updated Code: This works with a timeline that goes from 0-1 in two seconds 25 | 26 | // Sets default values 27 | AOpenDoorTimelineOverlap::AOpenDoorTimelineOverlap() 28 | { 29 | Open = false; 30 | 31 | MyBoxComponent = CreateDefaultSubobject(TEXT("My Box Component")); 32 | MyBoxComponent->InitBoxExtent(FVector(50,50,50)); 33 | RootComponent = MyBoxComponent; 34 | 35 | Door = CreateDefaultSubobject(TEXT("My Mesh")); 36 | Door->SetRelativeLocation(FVector(0.0f, 50.0f, -50.0f)); 37 | Door->SetupAttachment(RootComponent); 38 | 39 | MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AOpenDoorTimelineOverlap::OnOverlapBegin); 40 | MyBoxComponent->OnComponentEndOverlap.AddDynamic(this, &AOpenDoorTimelineOverlap::OnOverlapEnd); 41 | 42 | } 43 | 44 | // Called when the game starts or when spawned 45 | void AOpenDoorTimelineOverlap::BeginPlay() 46 | { 47 | Super::BeginPlay(); 48 | 49 | RotateValue = 90.0f; 50 | 51 | if (OpenCurve) 52 | { 53 | FOnTimelineFloat TimelineCallback; 54 | TimelineCallback.BindUFunction(this, FName("ControlDoor")); 55 | 56 | MyTimeline = NewObject(this, FName("DoorAnimation")); 57 | MyTimeline->AddInterpFloat(OpenCurve, TimelineCallback); 58 | MyTimeline->RegisterComponent(); 59 | } 60 | } 61 | 62 | void AOpenDoorTimelineOverlap::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) 63 | { 64 | if ( (OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr) ) 65 | { 66 | 67 | FVector PawnLocation = OtherActor->GetActorLocation(); 68 | FVector Direction = GetActorLocation() - PawnLocation; 69 | Direction = UKismetMathLibrary::LessLess_VectorRotator(Direction, GetActorRotation()); 70 | 71 | if(Direction.X > 0.0f) 72 | { 73 | RotateValue = 90.0f; 74 | } 75 | else 76 | { 77 | RotateValue = -90.0f; 78 | } 79 | 80 | DoorRotation = Door->RelativeRotation; 81 | Open = true; 82 | if (MyTimeline != NULL) 83 | { 84 | MyTimeline->PlayFromStart(); 85 | } 86 | } 87 | } 88 | 89 | void AOpenDoorTimelineOverlap::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) 90 | { 91 | if ( (OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr) ) 92 | { 93 | DoorRotation = Door->RelativeRotation; 94 | 95 | Open = false; 96 | if (MyTimeline != NULL) 97 | { 98 | MyTimeline->Reverse(); 99 | } 100 | } 101 | } 102 | 103 | void AOpenDoorTimelineOverlap::ControlDoor(float Value) 104 | { 105 | if(Open) 106 | { 107 | CurveFloatValue = RotateValue*Value; 108 | 109 | FQuat NewRotation = FQuat(FRotator(0.f, CurveFloatValue, 0.f)); 110 | 111 | if (GEngine) 112 | { 113 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Timeline Value: %f"), CurveFloatValue)); 114 | } 115 | 116 | Door->SetRelativeRotation(NewRotation); 117 | } 118 | else 119 | { 120 | CurveFloatValue = RotateValue*Value; 121 | 122 | FQuat NewRotation = FQuat(FRotator(0.f, CurveFloatValue, 0.f)); 123 | 124 | if (GEngine) 125 | { 126 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Timeline Value: %f"), CurveFloatValue)); 127 | } 128 | 129 | Door->SetRelativeRotation(NewRotation); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /OpenDoorTimelineOverlap/OpenDoorTimelineOverlap.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "OpenDoorTimelineOverlap.generated.h" 11 | 12 | class UBoxComponent; 13 | class UTimelineComponent; 14 | 15 | UCLASS() 16 | class UNREALCPP_API AOpenDoorTimelineOverlap : public AActor 17 | { 18 | GENERATED_BODY() 19 | 20 | public: 21 | // Sets default values for this actor's properties 22 | AOpenDoorTimelineOverlap(); 23 | 24 | protected: 25 | // Called when the game starts or when spawned 26 | virtual void BeginPlay() override; 27 | 28 | public: 29 | UPROPERTY(EditAnywhere) 30 | UStaticMeshComponent* Door; 31 | 32 | UPROPERTY(EditAnywhere) 33 | UBoxComponent* MyBoxComponent; 34 | 35 | UPROPERTY(EditAnywhere) 36 | UCurveFloat *OpenCurve; 37 | 38 | // declare overlap begin function 39 | UFUNCTION() 40 | void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); 41 | 42 | // declare overlap end function 43 | UFUNCTION() 44 | void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); 45 | 46 | UFUNCTION() 47 | void ControlDoor(float Value); 48 | 49 | bool Open; 50 | float RotateValue; 51 | float CurveFloatValue; 52 | FRotator DoorRotation; 53 | UTimelineComponent* MyTimeline; 54 | }; 55 | -------------------------------------------------------------------------------- /OpenDoorWithLerp/OpenDoorWithLerp.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | // helpful links 8 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/USceneComponent/index.html 9 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/OnComponentBeginOverlap/ 10 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/OnComponentEndOverlap/ 11 | // https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Vector/UnrotateVector/ 12 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Kismet/UKismetMathLibrary/LessLess_VectorRotator/index.html 13 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Kismet/UKismetMathLibrary/index.html 14 | 15 | #include "OpenDoorWithLerp.h" 16 | #include "Components/BoxComponent.h" 17 | #include "Kismet/KismetMathLibrary.h" 18 | 19 | // Sets default values 20 | AOpenDoorWithLerp::AOpenDoorWithLerp() 21 | { 22 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 23 | PrimaryActorTick.bCanEverTick = true; 24 | 25 | Open = false; 26 | 27 | MyBoxComponent = CreateDefaultSubobject(TEXT("My Box Component")); 28 | MyBoxComponent->InitBoxExtent(FVector(50,50,50)); 29 | RootComponent = MyBoxComponent; 30 | 31 | Door = CreateDefaultSubobject(TEXT("My Mesh")); 32 | Door->SetRelativeLocation(FVector(0.0f, 50.0f, -50.0f)); 33 | Door->SetupAttachment(RootComponent); 34 | 35 | MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AOpenDoorWithLerp::OnOverlapBegin); 36 | MyBoxComponent->OnComponentEndOverlap.AddDynamic(this, &AOpenDoorWithLerp::OnOverlapEnd); 37 | 38 | } 39 | 40 | // Called every frame 41 | void AOpenDoorWithLerp::Tick(float DeltaTime) 42 | { 43 | Super::Tick(DeltaTime); 44 | // maybe in UE5,RelativeRotation is private 45 | DoorRotation = Door->GetRelativeRotation(); 46 | 47 | if(Open) 48 | { 49 | Door->SetRelativeRotation(FMath::Lerp(FQuat(DoorRotation), FQuat(FRotator(0.0f, RotateValue, 0.0f)), 0.01f)); 50 | } 51 | else 52 | { 53 | Door->SetRelativeRotation(FMath::Lerp(FQuat(DoorRotation), FQuat(FRotator(0.0f, 0.0f, 0.0f)), 0.01f)); 54 | } 55 | 56 | } 57 | 58 | void AOpenDoorWithLerp::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) 59 | { 60 | if (OtherActor != nullptr && OtherActor != this && OtherComp != nullptr) 61 | { 62 | FVector PawnLocation = OtherActor->GetActorLocation(); 63 | FVector Direction = GetActorLocation() - PawnLocation; 64 | Direction = UKismetMathLibrary::LessLess_VectorRotator(Direction, GetActorRotation()); 65 | 66 | if(Direction.X > 0.0f) 67 | { 68 | RotateValue = 90.0f; 69 | } 70 | else 71 | { 72 | RotateValue = -90.0f; 73 | } 74 | 75 | Open = true; 76 | } 77 | } 78 | 79 | void AOpenDoorWithLerp::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) 80 | { 81 | if (OtherActor != nullptr && OtherActor != this && OtherComp != nullptr) 82 | { 83 | Open = false; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /OpenDoorWithLerp/OpenDoorWithLerp.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "OpenDoorWithLerp.generated.h" 11 | 12 | class UBoxComponent; 13 | 14 | UCLASS() 15 | class UNREALCPP_API AOpenDoorWithLerp : public AActor 16 | { 17 | GENERATED_BODY() 18 | 19 | public: 20 | // Sets default values for this actor's properties 21 | AOpenDoorWithLerp(); 22 | 23 | // Called every frame 24 | virtual void Tick(float DeltaTime) override; 25 | 26 | UPROPERTY(EditAnywhere) 27 | UStaticMeshComponent* Door; 28 | 29 | UPROPERTY(EditAnywhere) 30 | UBoxComponent* MyBoxComponent; 31 | 32 | // declare overlap begin function 33 | UFUNCTION() 34 | void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); 35 | 36 | // declare overlap end function 37 | UFUNCTION() 38 | void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); 39 | 40 | bool Open; 41 | float RotateValue; 42 | FRotator DoorRotation; 43 | 44 | }; 45 | -------------------------------------------------------------------------------- /PickupAndRotateActor/PickupAndRotateActor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | // Helpful Links 8 | // http://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UWorld/LineTraceSingleByChannel/index.html 9 | // http://api.unrealengine.com/INT/API/Runtime/Engine/FCollisionQueryParams/index.html 10 | // http://api.unrealengine.com/INT/API/Runtime/Engine/GameFramework/APawn/index.html 11 | 12 | #include "PickupAndRotateActor.h" 13 | #include "Camera/CameraComponent.h" 14 | #include "Kismet/GameplayStatics.h" 15 | #include "GameFramework/Character.h" 16 | 17 | // Sets default values 18 | APickupAndRotateActor::APickupAndRotateActor() 19 | { 20 | MyMesh = CreateDefaultSubobject(TEXT("My Mesh")); 21 | MyMesh->SetSimulatePhysics(true); 22 | RootComponent = MyMesh; 23 | 24 | bHolding = false; 25 | bGravity = true; 26 | } 27 | 28 | // Called when the game starts or when spawned 29 | void APickupAndRotateActor::BeginPlay() 30 | { 31 | Super::BeginPlay(); 32 | 33 | MyCharacter = UGameplayStatics::GetPlayerCharacter(this, 0); 34 | PlayerCamera = MyCharacter->FindComponentByClass(); 35 | 36 | TArray Components; 37 | 38 | MyCharacter->GetComponents(Components); 39 | 40 | if(Components.Num() > 0) 41 | { 42 | for (auto& Comp : Components) 43 | { 44 | if(Comp->GetName() == "HoldingComponent") 45 | { 46 | HoldingComp = Cast(Comp); 47 | } 48 | } 49 | } 50 | } 51 | 52 | void APickupAndRotateActor::RotateActor() 53 | { 54 | ControlRotation = GetWorld()->GetFirstPlayerController()->GetControlRotation(); 55 | SetActorRotation(FQuat(ControlRotation)); 56 | } 57 | 58 | void APickupAndRotateActor::Pickup() 59 | { 60 | bHolding = !bHolding; 61 | bGravity = !bGravity; 62 | MyMesh->SetEnableGravity(bGravity); 63 | MyMesh->SetSimulatePhysics(bHolding ? false : true); 64 | MyMesh->SetCollisionEnabled(bHolding ? ECollisionEnabled::NoCollision : ECollisionEnabled::QueryAndPhysics); 65 | if(HoldingComp && bHolding) 66 | { 67 | MyMesh->AttachToComponent(HoldingComp, FAttachmentTransformRules::KeepWorldTransform); 68 | SetActorLocation(HoldingComp->GetComponentLocation()); 69 | } 70 | 71 | if(!bHolding) 72 | { 73 | MyMesh->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform); 74 | ForwardVector = PlayerCamera->GetForwardVector(); 75 | MyMesh->AddForce(ForwardVector*100000*MyMesh->GetMass()); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /PickupAndRotateActor/PickupAndRotateActor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "PickupAndRotateActor.generated.h" 11 | 12 | class UCameraComponent; 13 | class ACharacter; 14 | 15 | UCLASS() 16 | class UNREALCPP_API APickupAndRotateActor : public AActor 17 | { 18 | GENERATED_BODY() 19 | 20 | public: 21 | // Sets default values for this actor's properties 22 | APickupAndRotateActor(); 23 | 24 | protected: 25 | // Called when the game starts or when spawned 26 | virtual void BeginPlay() override; 27 | 28 | public: 29 | UPROPERTY(EditAnywhere) 30 | UStaticMeshComponent* MyMesh; 31 | 32 | UPROPERTY(EditAnywhere) 33 | USceneComponent* HoldingComp; 34 | 35 | UFUNCTION() 36 | void RotateActor(); 37 | 38 | UFUNCTION() 39 | void Pickup(); 40 | 41 | bool bHolding; 42 | bool bGravity; 43 | 44 | FRotator ControlRotation; 45 | ACharacter* MyCharacter; 46 | UCameraComponent* PlayerCamera; 47 | FVector ForwardVector; 48 | }; 49 | -------------------------------------------------------------------------------- /PickupAndRotateActor/UnrealCPPCharacter.h.txt: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Character.h" 7 | #include "UnrealCPPCharacter.generated.h" 8 | 9 | class UInputComponent; 10 | class APickupAndRotateActor; 11 | 12 | UCLASS(config=Game) 13 | class AUnrealCPPCharacter : public ACharacter 14 | { 15 | GENERATED_BODY() 16 | 17 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 18 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 19 | class USkeletalMeshComponent* Mesh1P; 20 | 21 | /** Gun mesh: 1st person view (seen only by self) */ 22 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 23 | class USkeletalMeshComponent* FP_Gun; 24 | 25 | /** Location on gun mesh where projectiles should spawn. */ 26 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 27 | class USceneComponent* FP_MuzzleLocation; 28 | 29 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 30 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 31 | class USkeletalMeshComponent* VR_Gun; 32 | 33 | /** Location on VR gun mesh where projectiles should spawn. */ 34 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 35 | class USceneComponent* VR_MuzzleLocation; 36 | 37 | /** First person camera */ 38 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 39 | class UCameraComponent* FirstPersonCameraComponent; 40 | 41 | /** Motion controller (right hand) */ 42 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 43 | class UMotionControllerComponent* R_MotionController; 44 | 45 | /** Motion controller (left hand) */ 46 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 47 | class UMotionControllerComponent* L_MotionController; 48 | 49 | /** Holding Component */ 50 | UPROPERTY(EditAnywhere) 51 | class USceneComponent* HoldingComponent; 52 | 53 | public: 54 | AUnrealCPPCharacter(); 55 | 56 | protected: 57 | virtual void BeginPlay(); 58 | 59 | virtual void Tick(float DeltaSeconds) override; 60 | 61 | public: 62 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 63 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 64 | float BaseTurnRate; 65 | 66 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 67 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 68 | float BaseLookUpRate; 69 | 70 | /** Gun muzzle's offset from the characters location */ 71 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 72 | FVector GunOffset; 73 | 74 | /** Projectile class to spawn */ 75 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 76 | TSubclassOf ProjectileClass; 77 | 78 | /** Sound to play each time we fire */ 79 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 80 | class USoundBase* FireSound; 81 | 82 | /** AnimMontage to play each time we fire */ 83 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 84 | class UAnimMontage* FireAnimation; 85 | 86 | /** Whether to use motion controller location for aiming. */ 87 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 88 | uint32 bUsingMotionControllers : 1; 89 | 90 | UPROPERTY(EditAnywhere) 91 | class APickupAndRotateActor* CurrentItem; 92 | 93 | bool bCanMove; 94 | bool bHoldingItem; 95 | bool bInspecting; 96 | 97 | float PitchMax; 98 | float PitchMin; 99 | 100 | FVector HoldingComp; 101 | FRotator LastRotation; 102 | 103 | FVector Start; 104 | FVector ForwardVector; 105 | FVector End; 106 | 107 | FHitResult Hit; 108 | 109 | FComponentQueryParams DefaultComponentQueryParams; 110 | FCollisionResponseParams DefaultResponseParam; 111 | 112 | protected: 113 | 114 | /** Fires a projectile. */ 115 | void OnFire(); 116 | 117 | /** Action Function */ 118 | void OnAction(); 119 | 120 | /** Inspect Function */ 121 | void OnInspect(); 122 | void OnInspectReleased(); 123 | 124 | /** Resets HMD orientation and position in VR. */ 125 | void OnResetVR(); 126 | 127 | /** Handles moving forward/backward */ 128 | void MoveForward(float Val); 129 | 130 | /** Handles stafing movement, left and right */ 131 | void MoveRight(float Val); 132 | 133 | /** 134 | * Called via input to turn at a given rate. 135 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 136 | */ 137 | void TurnAtRate(float Rate); 138 | 139 | /** 140 | * Called via input to turn look up/down at a given rate. 141 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 142 | */ 143 | void LookUpAtRate(float Rate); 144 | 145 | struct TouchData 146 | { 147 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 148 | bool bIsPressed; 149 | ETouchIndex::Type FingerIndex; 150 | FVector Location; 151 | bool bMoved; 152 | }; 153 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 154 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 155 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 156 | TouchData TouchItem; 157 | 158 | // toggle player movement 159 | void ToggleMovement(); 160 | 161 | // toggle holding item pickup 162 | void ToggleItemPickup(); 163 | 164 | protected: 165 | // APawn interface 166 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 167 | // End of APawn interface 168 | 169 | /* 170 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 171 | * 172 | * @param InputComponent The input component pointer to bind controls to 173 | * @returns true if touch controls were enabled. 174 | */ 175 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 176 | 177 | public: 178 | /** Returns Mesh1P subobject **/ 179 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 180 | /** Returns FirstPersonCameraComponent subobject **/ 181 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 182 | 183 | }; 184 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [unrealcpp.com](https://unrealcpp.com/) 2 | 3 | ## Visit the [GD Tactics Unreal repo](https://github.com/Harrison1/gdtactics-unreal) for the latest 4 | 5 | This repo holds all the code for the [unrealcpp.com](https://unrealcpp.com/) tutorials. 6 | 7 | Each folder is desigend to be its own class. I have some files that are `.txt` to prevent errors while I work locally. So if there is a `.txt` file it's typically meant to be a `.h` or `.cpp` file. 8 | 9 | [![screenshot](https://res.cloudinary.com/dz09rnbhe/image/upload/v1624534611/unrealcpp/screenshot_r4yrhw.png "screenshot")](https://unrealcpp.com) 10 | -------------------------------------------------------------------------------- /RotateActorAroundPlayer/RotateActorAroundPlayer.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "RotateActorAroundPlayer.h" 8 | 9 | 10 | // Sets default values 11 | ARotateActorAroundPlayer::ARotateActorAroundPlayer() 12 | { 13 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 14 | PrimaryActorTick.bCanEverTick = true; 15 | 16 | Dimensions = FVector (300, 0, 0); 17 | AxisVector = FVector (0, 0, 1); 18 | Multiplier = 50.f; 19 | } 20 | 21 | // Called every frame 22 | void ARotateActorAroundPlayer::Tick(float DeltaTime) 23 | { 24 | Super::Tick(DeltaTime); 25 | 26 | FVector NewLocation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation(); 27 | 28 | AngleAxis += DeltaTime * Multiplier; 29 | 30 | if(AngleAxis >= 360.0f) 31 | { 32 | AngleAxis = 0; 33 | } 34 | 35 | FVector RotateValue = Dimensions.RotateAngleAxis(AngleAxis, AxisVector); 36 | 37 | NewLocation.X += RotateValue.X; 38 | NewLocation.Y += RotateValue.Y; 39 | NewLocation.Z += RotateValue.Z; 40 | 41 | SetActorLocation(NewLocation, false, 0, ETeleportType::None); 42 | } 43 | -------------------------------------------------------------------------------- /RotateActorAroundPlayer/RotateActorAroundPlayer.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "RotateActorAroundPlayer.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ARotateActorAroundPlayer : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | ARotateActorAroundPlayer(); 20 | 21 | // Called every frame 22 | virtual void Tick(float DeltaTime) override; 23 | 24 | // declare our float variables 25 | UPROPERTY(EditAnywhere, Category = Movement) 26 | float AngleAxis; 27 | 28 | UPROPERTY(EditAnywhere, Category = Movement) 29 | FVector Dimensions; 30 | 31 | UPROPERTY(EditAnywhere, Category = Movement) 32 | FVector AxisVector; 33 | 34 | UPROPERTY(EditAnywhere, Category = Movement) 35 | float Multiplier; 36 | }; 37 | -------------------------------------------------------------------------------- /RotateActorAroundWithRotation/RotateActorAroundWithRotation.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "RotateActorAroundWithRotation.h" 8 | 9 | 10 | // Sets default values 11 | ARotateActorAroundWithRotation::ARotateActorAroundWithRotation() 12 | { 13 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 14 | PrimaryActorTick.bCanEverTick = true; 15 | 16 | Dimensions = FVector (300, 0, 0); 17 | AxisVector = FVector (0, 0, 1); 18 | Multiplier = 50.f; 19 | } 20 | 21 | // Called every frame 22 | void ARotateActorAroundWithRotation::Tick(float DeltaTime) 23 | { 24 | Super::Tick(DeltaTime); 25 | 26 | FVector NewLocation = FVector (0,0,400); 27 | 28 | // rotate around player 29 | // FVector NewLocation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation(); 30 | 31 | AngleAxis += DeltaTime * Multiplier; 32 | 33 | if(AngleAxis >= 360.0f) 34 | { 35 | AngleAxis = 0; 36 | } 37 | 38 | FVector RotateValue = Dimensions.RotateAngleAxis(AngleAxis, AxisVector); 39 | 40 | NewLocation.X += RotateValue.X; 41 | NewLocation.Y += RotateValue.Y; 42 | NewLocation.Z += RotateValue.Z; 43 | 44 | FRotator NewRotation = FRotator(0, AngleAxis, 0); 45 | 46 | FQuat QuatRotation = FQuat(NewRotation); 47 | 48 | SetActorLocationAndRotation(NewLocation, QuatRotation, false, 0, ETeleportType::None); 49 | 50 | } 51 | 52 | -------------------------------------------------------------------------------- /RotateActorAroundWithRotation/RotateActorAroundWithRotation.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "RotateActorAroundWithRotation.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ARotateActorAroundWithRotation : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | ARotateActorAroundWithRotation(); 20 | 21 | // Called every frame 22 | virtual void Tick(float DeltaTime) override; 23 | 24 | // declare our float variables 25 | UPROPERTY(EditAnywhere, Category = Movement) 26 | float AngleAxis; 27 | 28 | UPROPERTY(EditAnywhere, Category = Movement) 29 | FVector Dimensions; 30 | 31 | UPROPERTY(EditAnywhere, Category = Movement) 32 | FVector AxisVector; 33 | 34 | UPROPERTY(EditAnywhere, Category = Movement) 35 | float Multiplier; 36 | 37 | }; 38 | -------------------------------------------------------------------------------- /RotateAngleAxis/RotatingAngleAxis.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "RotatingAngleAxis.h" 8 | 9 | 10 | // Sets default values 11 | ARotatingAngleAxis::ARotatingAngleAxis() 12 | { 13 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 14 | PrimaryActorTick.bCanEverTick = true; 15 | 16 | Dimensions = FVector (300, 0, 0); 17 | AxisVector = FVector (0, 0, 1); 18 | Multiplier = 50.f; 19 | } 20 | 21 | // Called every frame 22 | void ARotatingAngleAxis::Tick(float DeltaTime) 23 | { 24 | Super::Tick(DeltaTime); 25 | 26 | FVector NewLocation = FVector (0,0,800); 27 | 28 | AngleAxis += DeltaTime * Multiplier; 29 | 30 | if(AngleAxis >= 360.0f) AngleAxis = 0; 31 | 32 | FVector RotateValue = Dimensions.RotateAngleAxis(AngleAxis, AxisVector); 33 | 34 | if(GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("RotateValue: %s"), *RotateValue.ToString())); 35 | if(GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("AngleAxis: %f"), AngleAxis)); 36 | 37 | NewLocation.X += RotateValue.X; 38 | NewLocation.Y += RotateValue.Y; 39 | NewLocation.Z += RotateValue.Z; 40 | 41 | SetActorLocation(NewLocation, false, 0, ETeleportType::None); 42 | } 43 | -------------------------------------------------------------------------------- /RotateAngleAxis/RotatingAngleAxis.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "RotatingAngleAxis.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ARotatingAngleAxis : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | ARotatingAngleAxis(); 20 | 21 | // Called every frame 22 | virtual void Tick(float DeltaTime) override; 23 | 24 | // declare our float variables 25 | UPROPERTY(EditAnywhere, Category = Movement) 26 | float AngleAxis; 27 | 28 | UPROPERTY(EditAnywhere, Category = Movement) 29 | FVector Dimensions; 30 | 31 | UPROPERTY(EditAnywhere, Category = Movement) 32 | FVector AxisVector; 33 | 34 | UPROPERTY(EditAnywhere, Category = Movement) 35 | float Multiplier; 36 | 37 | }; 38 | -------------------------------------------------------------------------------- /RotateAroundVector/RotateAroundVector.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "RotateAroundVector.h" 8 | 9 | 10 | // Sets default values 11 | ARotateAroundVector::ARotateAroundVector() 12 | { 13 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 14 | PrimaryActorTick.bCanEverTick = true; 15 | 16 | AngleAxis = 0; 17 | } 18 | 19 | // Called every frame 20 | void ARotateAroundVector::Tick(float DeltaTime) 21 | { 22 | Super::Tick(DeltaTime); 23 | 24 | // declare arbitrary vector point in the world to circle around 25 | FVector NewLocation = FVector (0,0,800); 26 | 27 | // declare size of radius to move around 28 | FVector Radius = FVector(200,0,0); 29 | 30 | // angle increases by 1 every frame 31 | AngleAxis++; 32 | 33 | // prevent number from growind indefinitely 34 | if(AngleAxis > 360.0f) AngleAxis = 1; 35 | 36 | FVector RotateValue = Radius.RotateAngleAxis(AngleAxis, FVector (0,0,1)); 37 | 38 | NewLocation.X += RotateValue.X; 39 | NewLocation.Y += RotateValue.Y; 40 | NewLocation.Z += RotateValue.Z; 41 | 42 | SetActorLocation(NewLocation); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /RotateAroundVector/RotateAroundVector.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "RotateAroundVector.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ARotateAroundVector : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | ARotateAroundVector(); 20 | 21 | // Called every frame 22 | virtual void Tick(float DeltaTime) override; 23 | 24 | // declare our variable for the angle axis 25 | float AngleAxis; 26 | }; 27 | -------------------------------------------------------------------------------- /RotatingActor/RotatingActor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "RotatingActor.h" 8 | 9 | 10 | // Sets default values 11 | ARotatingActor::ARotatingActor() 12 | { 13 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 14 | PrimaryActorTick.bCanEverTick = true; 15 | 16 | PitchValue = 0.f; 17 | YawValue = 0.f; 18 | RollValue = 0.f; 19 | } 20 | 21 | // Called every frame 22 | void ARotatingActor::Tick(float DeltaTime) 23 | { 24 | Super::Tick(DeltaTime); 25 | 26 | // on every frame change rotationg for a smooth rotating actor 27 | FQuat QuatRotation = FRotator(PitchValue, YawValue, RollValue).Quaternion(); 28 | 29 | AddActorLocalRotation(QuatRotation, false, 0, ETeleportType::None); 30 | 31 | // You will want to set rotation with Quanternions and not Rotation values 32 | // The below method causes a bug for the pitch value. The pitch value stops updating at 90 degrees due to Gimbal Lock 33 | 34 | // FRotator NewRotation = GetActorRotation(); 35 | 36 | // NewRotation.Pitch += PitchValue; 37 | // NewRotation.Yaw += YawValue; 38 | // NewRotation.Roll += RollValue; 39 | 40 | // OR add values using the FRotator Add function 41 | // FRotator NewRotation = GetActorRotation().Add(PitchValue, YawValue, RollValue); 42 | 43 | // SetActorRotation(NewRotation); 44 | } 45 | -------------------------------------------------------------------------------- /RotatingActor/RotatingActor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "RotatingActor.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ARotatingActor : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | ARotatingActor(); 20 | 21 | // Called every frame 22 | virtual void Tick(float DeltaTime) override; 23 | 24 | // declare our float variables 25 | UPROPERTY(EditAnywhere, Category = Movement) 26 | float PitchValue; 27 | 28 | UPROPERTY(EditAnywhere, Category = Movement) 29 | float YawValue; 30 | 31 | UPROPERTY(EditAnywhere, Category = Movement) 32 | float RollValue; 33 | }; 34 | -------------------------------------------------------------------------------- /SetActorLocationAndRotation/SetActorLocationAndRotation.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "SetActorLocationAndRotation.h" 8 | 9 | // Called when the game starts or when spawned 10 | void ASetActorLocationAndRotation::BeginPlay() 11 | { 12 | Super::BeginPlay(); 13 | 14 | SetActorLocationAndRotation(NewLocation, NewRotation, false, 0, ETeleportType::None); 15 | } 16 | -------------------------------------------------------------------------------- /SetActorLocationAndRotation/SetActorLocationAndRotation.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "SetActorLocationAndRotation.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ASetActorLocationAndRotation : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | protected: 18 | // Called when the game starts or when spawned 19 | virtual void BeginPlay() override; 20 | 21 | public: 22 | UPROPERTY(EditAnywhere, Category = Location) 23 | FVector NewLocation; 24 | 25 | UPROPERTY(EditAnywhere, Category = Location) 26 | FQuat NewRotation; 27 | }; 28 | -------------------------------------------------------------------------------- /SetViewTarget/SetViewTarget.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "SetViewTarget.h" 8 | // include gameplay statics header file 9 | #include "Kismet/GameplayStatics.h" 10 | 11 | // Sets default values 12 | ASetViewTarget::ASetViewTarget() 13 | { 14 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 15 | PrimaryActorTick.bCanEverTick = true; 16 | } 17 | 18 | // Called when the game starts or when spawned 19 | void ASetViewTarget::BeginPlay() 20 | { 21 | Super::BeginPlay(); 22 | 23 | //Find the actor that handles control for the local player. 24 | APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0); 25 | 26 | //Cut instantly to our actor on begin play. 27 | OurPlayerController->SetViewTarget(MyActor); 28 | } 29 | -------------------------------------------------------------------------------- /SetViewTarget/SetViewTarget.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "SetViewTarget.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ASetViewTarget : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | ASetViewTarget(); 20 | 21 | protected: 22 | // Called when the game starts or when spawned 23 | virtual void BeginPlay() override; 24 | 25 | public: 26 | // declare variables 27 | UPROPERTY(EditAnywhere) 28 | AActor* MyActor; 29 | }; 30 | -------------------------------------------------------------------------------- /SetViewTargetBlend/SetViewTargetBlend.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "SetViewTargetBlend.h" 8 | // include gameplay statics header file 9 | #include "Kismet/GameplayStatics.h" 10 | 11 | // Called when the game starts or when spawned 12 | void ASetViewTargetBlend::BeginPlay() 13 | { 14 | Super::BeginPlay(); 15 | 16 | //Find the actor that handles control for the local player. 17 | APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0); 18 | 19 | //Smoothly transition to our actor on begin play. 20 | OurPlayerController->SetViewTargetWithBlend(MyActor, 2.f); 21 | } 22 | -------------------------------------------------------------------------------- /SetViewTargetBlend/SetViewTargetBlend.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "SetViewTargetBlend.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ASetViewTargetBlend : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | protected: 18 | // Called when the game starts or when spawned 19 | virtual void BeginPlay() override; 20 | 21 | public: 22 | // declare variables 23 | UPROPERTY(EditAnywhere) 24 | AActor* MyActor; 25 | 26 | }; 27 | -------------------------------------------------------------------------------- /ShooterProjectile/ShooterProjectile.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.3 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "ShooterProjectile.h" 8 | 9 | 10 | // Sets default values 11 | AShooterProjectile::AShooterProjectile() 12 | { 13 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 14 | PrimaryActorTick.bCanEverTick = true; 15 | 16 | } 17 | 18 | // Called when the game starts or when spawned 19 | void AShooterProjectile::BeginPlay() 20 | { 21 | Super::BeginPlay(); 22 | 23 | } 24 | 25 | // Called every frame 26 | void AShooterProjectile::Tick(float DeltaTime) 27 | { 28 | Super::Tick(DeltaTime); 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /ShooterProjectile/ShooterProjectile.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.3 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "ShooterProjectile.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AShooterProjectile : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | AShooterProjectile(); 20 | 21 | protected: 22 | // Called when the game starts or when spawned 23 | virtual void BeginPlay() override; 24 | 25 | public: 26 | // Called every frame 27 | virtual void Tick(float DeltaTime) override; 28 | 29 | 30 | 31 | }; 32 | -------------------------------------------------------------------------------- /SwingDoor/SwingDoor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.24.2 3 | // https://github.com/Harrison1 4 | // https://unrealcpp.com 5 | 6 | // helpful links 7 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/SetActorRelativeRotation/2/index.html 8 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/USceneComponent/GetComponentRotation/index.html 9 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Kismet/UKismetSystemLibrary/DrawDebugBox/ 10 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/DrawDebugBox/2/ 11 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Math/FVector/DotProduct/index.html 12 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Math/FMath/ 13 | // https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Float/Sign_float/ 14 | 15 | #include "SwingDoor.h" 16 | #include "UObject/ConstructorHelpers.h" 17 | #include "Components/BoxComponent.h" 18 | #include "DrawDebugHelpers.h" 19 | #include "Kismet/GameplayStatics.h" 20 | 21 | // Sets default values 22 | ASwingDoor::ASwingDoor() 23 | { 24 | // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. 25 | PrimaryActorTick.bCanEverTick = true; 26 | 27 | BoxComp = CreateDefaultSubobject(TEXT("My Box Component")); 28 | BoxComp->InitBoxExtent(FVector(150, 100, 100)); 29 | BoxComp->SetCollisionProfileName("Trigger"); 30 | RootComponent = BoxComp; 31 | 32 | Door = CreateDefaultSubobject(TEXT("Door")); 33 | Door->SetupAttachment(RootComponent); 34 | 35 | static ConstructorHelpers::FObjectFinder DoorAsset(TEXT("/Game/StarterContent/Props/SM_Door.SM_Door")); 36 | 37 | if (DoorAsset.Succeeded()) 38 | { 39 | Door->SetStaticMesh(DoorAsset.Object); 40 | Door->SetRelativeLocation(FVector(0.0f, 50.0f, -100.0f)); 41 | Door->SetWorldScale3D(FVector(1.f)); 42 | } 43 | 44 | } 45 | 46 | // Called when the game starts or when spawned 47 | void ASwingDoor::BeginPlay() 48 | { 49 | Super::BeginPlay(); 50 | 51 | DrawDebugBox(GetWorld(), GetActorLocation(), BoxComp->GetScaledBoxExtent(), FQuat(GetActorRotation()), FColor::Turquoise, true, 999, 0, 2); 52 | } 53 | 54 | // Called every frame 55 | void ASwingDoor::Tick(float DeltaTime) 56 | { 57 | Super::Tick(DeltaTime); 58 | 59 | if (Opening) 60 | { 61 | OpenDoor(DeltaTime); 62 | } 63 | 64 | if (Closing) 65 | { 66 | CloseDoor(DeltaTime); 67 | } 68 | 69 | } 70 | 71 | void ASwingDoor::OpenDoor(float dt) 72 | { 73 | DoorCurrentRotation = Door->RelativeRotation.Yaw; 74 | 75 | AddRotation = PosNeg * dt * 80; 76 | 77 | if (FMath::IsNearlyEqual(DoorCurrentRotation, MaxDegree, 1.5f)) 78 | { 79 | Closing = false; 80 | Opening = false; 81 | } 82 | else if (Opening) 83 | { 84 | FRotator NewRotation = FRotator(0.0f, AddRotation, 0.0f); 85 | Door->AddRelativeRotation(FQuat(NewRotation), false, 0, ETeleportType::None); 86 | } 87 | } 88 | 89 | void ASwingDoor::CloseDoor(float dt) 90 | { 91 | DoorCurrentRotation = Door->RelativeRotation.Yaw; 92 | 93 | if (DoorCurrentRotation > 0) 94 | { 95 | AddRotation = -dt * 80; 96 | } 97 | else 98 | { 99 | AddRotation = dt * 80; 100 | } 101 | 102 | if (FMath::IsNearlyEqual(DoorCurrentRotation, 0.0f, 1.5f)) 103 | { 104 | Closing = false; 105 | Opening = false; 106 | } 107 | else if (Closing) 108 | { 109 | FRotator NewRotation = FRotator(0.0f, AddRotation, 0.0f); 110 | Door->AddRelativeRotation(FQuat(NewRotation), false, 0, ETeleportType::None); 111 | } 112 | } 113 | 114 | void ASwingDoor::ToggleDoor(FVector ForwardVector) 115 | { 116 | 117 | // alternatively you can grab the froward vector from the character inside theis function, remember to #include "GameFramework/Character.h" and #include "Camera/CameraComponent.h" at the top of the script 118 | // ACharacter* OurPlayerController = UGameplayStatics::GetPlayerCharacter(this, 0); 119 | 120 | // UCameraComponent* PlayerCamera = OurPlayerController->FindComponentByClass(); 121 | 122 | // FVector ForwardVector = PlayerCamera->GetForwardVector(); 123 | 124 | // is the chacter in front or behind the door 125 | DotP = FVector::DotProduct(BoxComp->GetForwardVector(), ForwardVector); 126 | 127 | // get 1 or -1 from the the dot product 128 | PosNeg = FMath::Sign(DotP); 129 | 130 | // degree to clamp at 131 | MaxDegree = PosNeg * 90.0f; 132 | 133 | // toggle bools 134 | if (isClosed) { 135 | isClosed = false; 136 | Closing = false; 137 | Opening = true; 138 | 139 | } 140 | else { 141 | Opening = false; 142 | isClosed = true; 143 | Closing = true; 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /SwingDoor/SwingDoor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.24.2 3 | // https://github.com/Harrison1 4 | // https://unrealcpp.com 5 | 6 | #pragma once 7 | 8 | #include "CoreMinimal.h" 9 | #include "GameFramework/Actor.h" 10 | #include "SwingDoor.generated.h" 11 | 12 | UCLASS() 13 | class UCPP_API ASwingDoor : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | // Sets default values for this actor's properties 19 | ASwingDoor(); 20 | 21 | protected: 22 | // Called when the game starts or when spawned 23 | virtual void BeginPlay() override; 24 | 25 | public: 26 | // Called every frame 27 | virtual void Tick(float DeltaTime) override; 28 | 29 | UPROPERTY() 30 | class UStaticMeshComponent* Door; 31 | 32 | // declare sphere comp 33 | UPROPERTY(VisibleAnywhere, Category = "BoxComp") 34 | class UBoxComponent* BoxComp; 35 | 36 | bool Opening = false; 37 | bool Closing = false; 38 | bool isClosed = true; 39 | 40 | float DotP = 0.f; 41 | float MaxDegree = 0.f; 42 | float AddRotation = 0.f; 43 | float PosNeg = 0.f; 44 | float DoorCurrentRotation = 0.f; 45 | 46 | UFUNCTION() 47 | void OpenDoor(float dt); 48 | 49 | UFUNCTION() 50 | void CloseDoor(float dt); 51 | 52 | UFUNCTION() 53 | void ToggleDoor(FVector ForwardVector); 54 | }; 55 | -------------------------------------------------------------------------------- /SwingDoor/UCPPCharacter.h: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2019 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "GameFramework/Character.h" 7 | #include "UCPPCharacter.generated.h" 8 | 9 | class UInputComponent; 10 | 11 | UCLASS(config=Game) 12 | class AUCPPCharacter : public ACharacter 13 | { 14 | GENERATED_BODY() 15 | 16 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 17 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 18 | class USkeletalMeshComponent* Mesh1P; 19 | 20 | /** Gun mesh: 1st person view (seen only by self) */ 21 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 22 | class USkeletalMeshComponent* FP_Gun; 23 | 24 | /** Location on gun mesh where projectiles should spawn. */ 25 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 26 | class USceneComponent* FP_MuzzleLocation; 27 | 28 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 29 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 30 | class USkeletalMeshComponent* VR_Gun; 31 | 32 | /** Location on VR gun mesh where projectiles should spawn. */ 33 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 34 | class USceneComponent* VR_MuzzleLocation; 35 | 36 | /** First person camera */ 37 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 38 | class UCameraComponent* FirstPersonCameraComponent; 39 | 40 | /** Motion controller (right hand) */ 41 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 42 | class UMotionControllerComponent* R_MotionController; 43 | 44 | /** Motion controller (left hand) */ 45 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 46 | class UMotionControllerComponent* L_MotionController; 47 | 48 | // create trigger capsule 49 | UPROPERTY(VisibleAnywhere, Category = "Trigger Capsule") 50 | class UCapsuleComponent* TriggerCapsule; 51 | 52 | 53 | public: 54 | AUCPPCharacter(); 55 | 56 | protected: 57 | virtual void BeginPlay(); 58 | 59 | public: 60 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 61 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 62 | float BaseTurnRate; 63 | 64 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 65 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 66 | float BaseLookUpRate; 67 | 68 | /** Gun muzzle's offset from the characters location */ 69 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 70 | FVector GunOffset; 71 | 72 | /** Projectile class to spawn */ 73 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 74 | TSubclassOf ProjectileClass; 75 | 76 | /** Sound to play each time we fire */ 77 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 78 | class USoundBase* FireSound; 79 | 80 | /** AnimMontage to play each time we fire */ 81 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 82 | class UAnimMontage* FireAnimation; 83 | 84 | /** Whether to use motion controller location for aiming. */ 85 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 86 | uint32 bUsingMotionControllers : 1; 87 | 88 | // declare overlap begin function 89 | UFUNCTION() 90 | void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); 91 | 92 | // declare overlap end function 93 | UFUNCTION() 94 | void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); 95 | 96 | UPROPERTY() 97 | class ASwingDoor* CurrentDoor; 98 | 99 | protected: 100 | 101 | /** Fires a projectile. */ 102 | void OnFire(); 103 | 104 | /** on Action */ 105 | void OnAction(); 106 | 107 | /** Resets HMD orientation and position in VR. */ 108 | void OnResetVR(); 109 | 110 | /** Handles moving forward/backward */ 111 | void MoveForward(float Val); 112 | 113 | /** Handles stafing movement, left and right */ 114 | void MoveRight(float Val); 115 | 116 | /** 117 | * Called via input to turn at a given rate. 118 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 119 | */ 120 | void TurnAtRate(float Rate); 121 | 122 | /** 123 | * Called via input to turn look up/down at a given rate. 124 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 125 | */ 126 | void LookUpAtRate(float Rate); 127 | 128 | struct TouchData 129 | { 130 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 131 | bool bIsPressed; 132 | ETouchIndex::Type FingerIndex; 133 | FVector Location; 134 | bool bMoved; 135 | }; 136 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 137 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 138 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 139 | TouchData TouchItem; 140 | 141 | protected: 142 | // APawn interface 143 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 144 | // End of APawn interface 145 | 146 | /* 147 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 148 | * 149 | * @param InputComponent The input component pointer to bind controls to 150 | * @returns true if touch controls were enabled. 151 | */ 152 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 153 | 154 | public: 155 | /** Returns Mesh1P subobject **/ 156 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 157 | /** Returns FirstPersonCameraComponent subobject **/ 158 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 159 | 160 | }; 161 | -------------------------------------------------------------------------------- /TimerActor/TimerActor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | // helpful link 8 | // https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/FTimerManager/SetTimer/4/ 9 | 10 | #include "TimerActor.h" 11 | 12 | // Called when the game starts or when spawned 13 | void ATimerActor::BeginPlay() 14 | { 15 | Super::BeginPlay(); 16 | 17 | // connect timer function to actor. After 5 seconds run RepeatingFunction every 2 seconds 18 | GetWorldTimerManager().SetTimer(MemberTimerHandle, this, &ATimerActor::RepeatingFunction, 2.0f, true, 5.0f); 19 | } 20 | 21 | void ATimerActor::RepeatingFunction() 22 | { 23 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Hello Timer")); 24 | } 25 | -------------------------------------------------------------------------------- /TimerActor/TimerActor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "GameFramework/Actor.h" 10 | #include "TimerActor.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ATimerActor : public AActor 14 | { 15 | GENERATED_BODY() 16 | 17 | protected: 18 | // Called when the game starts or when spawned 19 | virtual void BeginPlay() override; 20 | 21 | public: 22 | void RepeatingFunction(); 23 | 24 | struct FTimerHandle MemberTimerHandle; 25 | }; 26 | -------------------------------------------------------------------------------- /TriggerBox/MyTriggerBox.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MyTriggerBox.h" 8 | // include draw debu helpers header file 9 | #include "DrawDebugHelpers.h" 10 | 11 | AMyTriggerBox::AMyTriggerBox() 12 | { 13 | //Register Events 14 | OnActorBeginOverlap.AddDynamic(this, &AMyTriggerBox::OnOverlapBegin); 15 | OnActorEndOverlap.AddDynamic(this, &AMyTriggerBox::OnOverlapEnd); 16 | } 17 | 18 | // Called when the game starts or when spawned 19 | void AMyTriggerBox::BeginPlay() 20 | { 21 | Super::BeginPlay(); 22 | 23 | DrawDebugBox(GetWorld(), GetActorLocation(), GetComponentsBoundingBox().GetExtent(), FColor::Purple, true, 999, 0, 5); 24 | } 25 | 26 | void AMyTriggerBox::OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor) 27 | { 28 | // check if Actors do not equal nullptr and that 29 | if (OtherActor && OtherActor != this) 30 | { 31 | if (GEngine) 32 | { 33 | GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, TEXT("Overlap Begin")); 34 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT("Overlapped Actor = %s"), *OverlappedActor->GetName())); 35 | } 36 | } 37 | } 38 | 39 | void AMyTriggerBox::OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor) 40 | { 41 | if (OtherActor && OtherActor != this) 42 | { 43 | if (GEngine) 44 | { 45 | GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, TEXT("Overlap Ended")); 46 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT("%s has left the Trigger Box"), *OtherActor->GetName())); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /TriggerBox/MyTriggerBox.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "Engine/TriggerBox.h" 10 | #include "MyTriggerBox.generated.h" 11 | 12 | 13 | UCLASS() 14 | class UNREALCPP_API AMyTriggerBox : public ATriggerBox 15 | { 16 | GENERATED_BODY() 17 | 18 | protected: 19 | // Called when the game starts or when spawned 20 | virtual void BeginPlay() override; 21 | 22 | public: 23 | // constructor sets default values for this actor's properties 24 | AMyTriggerBox(); 25 | 26 | // overlap begin function 27 | UFUNCTION() 28 | void OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor); 29 | 30 | // overlap end function 31 | UFUNCTION() 32 | void OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor); 33 | }; 34 | -------------------------------------------------------------------------------- /TriggerBoxSpecificActor/TriggerBoxSpecificActor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "TriggerBoxSpecificActor.h" 8 | // include draw debug helpers header file 9 | #include "DrawDebugHelpers.h" 10 | 11 | ATriggerBoxSpecificActor::ATriggerBoxSpecificActor() 12 | { 13 | //Register Events 14 | OnActorBeginOverlap.AddDynamic(this, &ATriggerBoxSpecificActor::OnOverlapBegin); 15 | OnActorEndOverlap.AddDynamic(this, &ATriggerBoxSpecificActor::OnOverlapEnd); 16 | } 17 | 18 | // Called when the game starts or when spawned 19 | void ATriggerBoxSpecificActor::BeginPlay() 20 | { 21 | Super::BeginPlay(); 22 | 23 | DrawDebugBox(GetWorld(), GetActorLocation(), GetComponentsBoundingBox().GetExtent(), FColor::Green, true, 999, 0, 5); 24 | } 25 | 26 | void ATriggerBoxSpecificActor::OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor) 27 | { 28 | //if the overlapping actor is the specific actor we identified in the editor 29 | if (OtherActor && OtherActor != this && OtherActor == SpecificActor ) 30 | { 31 | if (GEngine) 32 | { 33 | GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, TEXT("Overlap Begin")); 34 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT("Overlapping Actor = %s"), *OverlappedActor->GetName())); 35 | } 36 | } 37 | } 38 | 39 | void ATriggerBoxSpecificActor::OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor) 40 | { 41 | //if the overlapping actor is the specific actor we identified in the editor 42 | if (OtherActor && OtherActor != this && OtherActor == SpecificActor ) 43 | { 44 | if (GEngine) 45 | { 46 | GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, TEXT("Overlap Ended")); 47 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT("%s has left the Trigger Box"), *OtherActor->GetName())); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /TriggerBoxSpecificActor/TriggerBoxSpecificActor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "Engine/TriggerBox.h" 10 | #include "TriggerBoxSpecificActor.generated.h" 11 | 12 | 13 | UCLASS() 14 | class UNREALCPP_API ATriggerBoxSpecificActor : public ATriggerBox 15 | { 16 | GENERATED_BODY() 17 | 18 | protected: 19 | 20 | // Called when the game starts or when spawned 21 | virtual void BeginPlay() override; 22 | 23 | public: 24 | // constructor sets default values for this actor's properties 25 | ATriggerBoxSpecificActor(); 26 | 27 | // overlap begin function 28 | UFUNCTION() 29 | void OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor); 30 | 31 | // overlap end function 32 | UFUNCTION() 33 | void OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor); 34 | 35 | // specific actor for overlap 36 | UPROPERTY(EditAnywhere) 37 | class AActor* SpecificActor; 38 | }; 39 | -------------------------------------------------------------------------------- /TriggerVolume/MyTriggerVolume.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "MyTriggerVolume.h" 8 | // include draw debug helpers header file 9 | #include "DrawDebugHelpers.h" 10 | 11 | AMyTriggerVolume::AMyTriggerVolume() 12 | { 13 | //Register Events 14 | OnActorBeginOverlap.AddDynamic(this, &AMyTriggerVolume::OnOverlapBegin); 15 | OnActorEndOverlap.AddDynamic(this, &AMyTriggerVolume::OnOverlapEnd); 16 | } 17 | 18 | // Called when the game starts or when spawned 19 | void AMyTriggerVolume::BeginPlay() 20 | { 21 | Super::BeginPlay(); 22 | 23 | DrawDebugBox(GetWorld(), GetActorLocation(), GetComponentsBoundingBox().GetExtent(), FColor::Purple, true, 999, 0, 5); 24 | } 25 | 26 | void AMyTriggerVolume::OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor) 27 | { 28 | // check if Actors do not equal nullptr and that 29 | if (OtherActor && OtherActor != this) 30 | { 31 | if (GEngine) 32 | { 33 | GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, TEXT("Overlap Begin")); 34 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT("Overlapped Actor = %s"), *OverlappedActor->GetName())); 35 | } 36 | } 37 | } 38 | 39 | void AMyTriggerVolume::OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor) 40 | { 41 | if (OtherActor && OtherActor != this) 42 | { 43 | if (GEngine) 44 | { 45 | GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, TEXT("Overlap Ended")); 46 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT("%s has left the Trigger Volume"), *OtherActor->GetName())); 47 | } 48 | } 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /TriggerVolume/MyTriggerVolume.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "Engine/TriggerVolume.h" 10 | #include "MyTriggerVolume.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API AMyTriggerVolume : public ATriggerVolume 14 | { 15 | GENERATED_BODY() 16 | 17 | protected: 18 | // Called when the game starts or when spawned 19 | virtual void BeginPlay() override; 20 | 21 | public: 22 | // constructor sets default values for this actor's properties 23 | AMyTriggerVolume(); 24 | 25 | // overlap begin function 26 | UFUNCTION() 27 | void OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor); 28 | 29 | // overlap end function 30 | UFUNCTION() 31 | void OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor); 32 | }; 33 | -------------------------------------------------------------------------------- /TriggerVolumeSpecificActor/TriggerVolumeSpecificActor.cpp: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #include "TriggerVolumeSpecificActor.h" 8 | // include draw debug helpers header file 9 | #include "DrawDebugHelpers.h" 10 | 11 | ATriggerVolumeSpecificActor::ATriggerVolumeSpecificActor() 12 | { 13 | //Register Events 14 | OnActorBeginOverlap.AddDynamic(this, &ATriggerVolumeSpecificActor::OnOverlapBegin); 15 | OnActorEndOverlap.AddDynamic(this, &ATriggerVolumeSpecificActor::OnOverlapEnd); 16 | } 17 | 18 | // Called when the game starts or when spawned 19 | void ATriggerVolumeSpecificActor::BeginPlay() 20 | { 21 | Super::BeginPlay(); 22 | 23 | DrawDebugBox(GetWorld(), GetActorLocation(), GetComponentsBoundingBox().GetExtent(), FColor::Green, true, 999, 0, 5); 24 | } 25 | 26 | void ATriggerVolumeSpecificActor::OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor) 27 | { 28 | //if the overlapping actor is the specific actor we identified in the editor 29 | if (OtherActor && OtherActor != this && OtherActor == SpecificActor ) 30 | { 31 | if (GEngine) 32 | { 33 | GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, TEXT("Overlap Begin")); 34 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT("Overlapping Actor = %s"), *OverlappedActor->GetName())); 35 | } 36 | } 37 | } 38 | 39 | void ATriggerVolumeSpecificActor::OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor) 40 | { 41 | //if the overlapping actor is the specific actor we identified in the editor 42 | if (OtherActor && OtherActor != this && OtherActor == SpecificActor ) 43 | { 44 | if (GEngine) 45 | { 46 | GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green, TEXT("Overlap Ended")); 47 | GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT("%s has left the Trigger Volume"), *OtherActor->GetName())); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /TriggerVolumeSpecificActor/TriggerVolumeSpecificActor.h: -------------------------------------------------------------------------------- 1 | // Harrison McGuire 2 | // UE4 Version 4.20.2 3 | // https://github.com/Harrison1/unrealcpp 4 | // https://severallevels.io 5 | // https://harrisonmcguire.com 6 | 7 | #pragma once 8 | 9 | #include "Engine/TriggerVolume.h" 10 | #include "TriggerVolumeSpecificActor.generated.h" 11 | 12 | UCLASS() 13 | class UNREALCPP_API ATriggerVolumeSpecificActor : public ATriggerVolume 14 | { 15 | GENERATED_BODY() 16 | 17 | protected: 18 | // Called when the game starts or when spawned 19 | virtual void BeginPlay() override; 20 | 21 | public: 22 | // constructor sets default values for this actor's properties 23 | ATriggerVolumeSpecificActor(); 24 | 25 | // overlap begin function 26 | UFUNCTION() 27 | void OnOverlapBegin(class AActor* OverlappedActor, class AActor* OtherActor); 28 | 29 | // overlap end function 30 | UFUNCTION() 31 | void OnOverlapEnd(class AActor* OverlappedActor, class AActor* OtherActor); 32 | 33 | // specific actor for overlap 34 | UPROPERTY(EditAnywhere) 35 | class AActor* SpecificActor; 36 | }; 37 | -------------------------------------------------------------------------------- /UnrealCPP.Build.cs: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | using UnrealBuildTool; 4 | 5 | public class UnrealCPP : ModuleRules 6 | { 7 | public UnrealCPP(ReadOnlyTargetRules Target) : base(Target) 8 | { 9 | PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; 10 | 11 | PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay" }); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /UnrealCPP.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealCPP.h" 4 | #include "Modules/ModuleManager.h" 5 | 6 | IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, UnrealCPP, "UnrealCPP" ); 7 | -------------------------------------------------------------------------------- /UnrealCPP.h: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | -------------------------------------------------------------------------------- /UnrealCPPCharacter.h: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "GameFramework/Character.h" 6 | #include "UnrealCPPCharacter.generated.h" 7 | 8 | class UInputComponent; 9 | 10 | UCLASS(config=Game) 11 | class AUnrealCPPCharacter : public ACharacter 12 | { 13 | GENERATED_BODY() 14 | 15 | /** Pawn mesh: 1st person view (arms; seen only by self) */ 16 | UPROPERTY(VisibleDefaultsOnly, Category=Mesh) 17 | class USkeletalMeshComponent* Mesh1P; 18 | 19 | /** Gun mesh: 1st person view (seen only by self) */ 20 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 21 | class USkeletalMeshComponent* FP_Gun; 22 | 23 | /** Location on gun mesh where projectiles should spawn. */ 24 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 25 | class USceneComponent* FP_MuzzleLocation; 26 | 27 | /** Gun mesh: VR view (attached to the VR controller directly, no arm, just the actual gun) */ 28 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 29 | class USkeletalMeshComponent* VR_Gun; 30 | 31 | /** Location on VR gun mesh where projectiles should spawn. */ 32 | UPROPERTY(VisibleDefaultsOnly, Category = Mesh) 33 | class USceneComponent* VR_MuzzleLocation; 34 | 35 | /** First person camera */ 36 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) 37 | class UCameraComponent* FirstPersonCameraComponent; 38 | 39 | /** Motion controller (right hand) */ 40 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 41 | class UMotionControllerComponent* R_MotionController; 42 | 43 | /** Motion controller (left hand) */ 44 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) 45 | class UMotionControllerComponent* L_MotionController; 46 | 47 | public: 48 | AUnrealCPPCharacter(); 49 | 50 | protected: 51 | virtual void BeginPlay(); 52 | 53 | public: 54 | /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ 55 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 56 | float BaseTurnRate; 57 | 58 | /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ 59 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) 60 | float BaseLookUpRate; 61 | 62 | /** Gun muzzle's offset from the characters location */ 63 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 64 | FVector GunOffset; 65 | 66 | /** Projectile class to spawn */ 67 | UPROPERTY(EditDefaultsOnly, Category=Projectile) 68 | TSubclassOf ProjectileClass; 69 | 70 | /** Sound to play each time we fire */ 71 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay) 72 | class USoundBase* FireSound; 73 | 74 | /** AnimMontage to play each time we fire */ 75 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 76 | class UAnimMontage* FireAnimation; 77 | 78 | /** Whether to use motion controller location for aiming. */ 79 | UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay) 80 | uint32 bUsingMotionControllers : 1; 81 | 82 | protected: 83 | 84 | /** Fires a projectile. */ 85 | void OnFire(); 86 | 87 | /** Resets HMD orientation and position in VR. */ 88 | void OnResetVR(); 89 | 90 | /** Handles moving forward/backward */ 91 | void MoveForward(float Val); 92 | 93 | /** Handles stafing movement, left and right */ 94 | void MoveRight(float Val); 95 | 96 | /** 97 | * Called via input to turn at a given rate. 98 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 99 | */ 100 | void TurnAtRate(float Rate); 101 | 102 | /** 103 | * Called via input to turn look up/down at a given rate. 104 | * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate 105 | */ 106 | void LookUpAtRate(float Rate); 107 | 108 | struct TouchData 109 | { 110 | TouchData() { bIsPressed = false;Location=FVector::ZeroVector;} 111 | bool bIsPressed; 112 | ETouchIndex::Type FingerIndex; 113 | FVector Location; 114 | bool bMoved; 115 | }; 116 | void BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 117 | void EndTouch(const ETouchIndex::Type FingerIndex, const FVector Location); 118 | void TouchUpdate(const ETouchIndex::Type FingerIndex, const FVector Location); 119 | TouchData TouchItem; 120 | 121 | protected: 122 | // APawn interface 123 | virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override; 124 | // End of APawn interface 125 | 126 | /* 127 | * Configures input for touchscreen devices if there is a valid touch interface for doing so 128 | * 129 | * @param InputComponent The input component pointer to bind controls to 130 | * @returns true if touch controls were enabled. 131 | */ 132 | bool EnableTouchscreenMovement(UInputComponent* InputComponent); 133 | 134 | public: 135 | /** Returns Mesh1P subobject **/ 136 | FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; } 137 | /** Returns FirstPersonCameraComponent subobject **/ 138 | FORCEINLINE class UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; } 139 | 140 | }; 141 | -------------------------------------------------------------------------------- /UnrealCPPGameMode.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealCPPGameMode.h" 4 | #include "UnrealCPPHUD.h" 5 | #include "UnrealCPPCharacter.h" 6 | #include "UObject/ConstructorHelpers.h" 7 | 8 | AUnrealCPPGameMode::AUnrealCPPGameMode() 9 | : Super() 10 | { 11 | // set default pawn class to our Blueprinted character 12 | static ConstructorHelpers::FClassFinder PlayerPawnClassFinder(TEXT("/Game/FirstPersonCPP/Blueprints/FirstPersonCharacter")); 13 | DefaultPawnClass = PlayerPawnClassFinder.Class; 14 | 15 | // use our custom HUD class 16 | HUDClass = AUnrealCPPHUD::StaticClass(); 17 | } 18 | -------------------------------------------------------------------------------- /UnrealCPPGameMode.h: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "GameFramework/GameModeBase.h" 6 | #include "UnrealCPPGameMode.generated.h" 7 | 8 | UCLASS(minimalapi) 9 | class AUnrealCPPGameMode : public AGameModeBase 10 | { 11 | GENERATED_BODY() 12 | 13 | public: 14 | AUnrealCPPGameMode(); 15 | }; 16 | -------------------------------------------------------------------------------- /UnrealCPPHUD.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealCPPHUD.h" 4 | #include "Engine/Canvas.h" 5 | #include "Engine/Texture2D.h" 6 | #include "TextureResource.h" 7 | #include "CanvasItem.h" 8 | #include "UObject/ConstructorHelpers.h" 9 | 10 | AUnrealCPPHUD::AUnrealCPPHUD() 11 | { 12 | // Set the crosshair texture 13 | static ConstructorHelpers::FObjectFinder CrosshairTexObj(TEXT("/Game/FirstPerson/Textures/FirstPersonCrosshair")); 14 | CrosshairTex = CrosshairTexObj.Object; 15 | } 16 | 17 | 18 | void AUnrealCPPHUD::DrawHUD() 19 | { 20 | Super::DrawHUD(); 21 | 22 | // Draw very simple crosshair 23 | 24 | // find center of the Canvas 25 | const FVector2D Center(Canvas->ClipX * 0.5f, Canvas->ClipY * 0.5f); 26 | 27 | // offset by half the texture's dimensions so that the center of the texture aligns with the center of the Canvas 28 | const FVector2D CrosshairDrawPosition( (Center.X), 29 | (Center.Y + 20.0f)); 30 | 31 | // draw the crosshair 32 | FCanvasTileItem TileItem( CrosshairDrawPosition, CrosshairTex->Resource, FLinearColor::White); 33 | TileItem.BlendMode = SE_BLEND_Translucent; 34 | Canvas->DrawItem( TileItem ); 35 | } 36 | -------------------------------------------------------------------------------- /UnrealCPPHUD.h: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "GameFramework/HUD.h" 6 | #include "UnrealCPPHUD.generated.h" 7 | 8 | UCLASS() 9 | class AUnrealCPPHUD : public AHUD 10 | { 11 | GENERATED_BODY() 12 | 13 | public: 14 | AUnrealCPPHUD(); 15 | 16 | /** Primary draw call for the HUD */ 17 | virtual void DrawHUD() override; 18 | 19 | private: 20 | /** Crosshair asset pointer */ 21 | class UTexture2D* CrosshairTex; 22 | }; 23 | -------------------------------------------------------------------------------- /UnrealCPPProjectile.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "UnrealCPPProjectile.h" 4 | #include "GameFramework/ProjectileMovementComponent.h" 5 | #include "Components/SphereComponent.h" 6 | 7 | AUnrealCPPProjectile::AUnrealCPPProjectile() 8 | { 9 | // Use a sphere as a simple collision representation 10 | CollisionComp = CreateDefaultSubobject(TEXT("SphereComp")); 11 | CollisionComp->InitSphereRadius(5.0f); 12 | CollisionComp->BodyInstance.SetCollisionProfileName("Projectile"); 13 | CollisionComp->OnComponentHit.AddDynamic(this, &AUnrealCPPProjectile::OnHit); // set up a notification for when this component hits something blocking 14 | 15 | // Players can't walk on it 16 | CollisionComp->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f)); 17 | CollisionComp->CanCharacterStepUpOn = ECB_No; 18 | 19 | // Set as root component 20 | RootComponent = CollisionComp; 21 | 22 | // Use a ProjectileMovementComponent to govern this projectile's movement 23 | ProjectileMovement = CreateDefaultSubobject(TEXT("ProjectileComp")); 24 | ProjectileMovement->UpdatedComponent = CollisionComp; 25 | ProjectileMovement->InitialSpeed = 3000.f; 26 | ProjectileMovement->MaxSpeed = 3000.f; 27 | ProjectileMovement->bRotationFollowsVelocity = true; 28 | ProjectileMovement->bShouldBounce = true; 29 | 30 | // Die after 3 seconds by default 31 | InitialLifeSpan = 3.0f; 32 | } 33 | 34 | void AUnrealCPPProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) 35 | { 36 | // Only add impulse and destroy projectile if we hit a physics 37 | if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL) && OtherComp->IsSimulatingPhysics()) 38 | { 39 | OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation()); 40 | 41 | Destroy(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /UnrealCPPProjectile.h: -------------------------------------------------------------------------------- 1 | // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "GameFramework/Actor.h" 6 | #include "UnrealCPPProjectile.generated.h" 7 | 8 | UCLASS(config=Game) 9 | class AUnrealCPPProjectile : public AActor 10 | { 11 | GENERATED_BODY() 12 | 13 | /** Sphere collision component */ 14 | UPROPERTY(VisibleDefaultsOnly, Category=Projectile) 15 | class USphereComponent* CollisionComp; 16 | 17 | /** Projectile movement component */ 18 | UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true")) 19 | class UProjectileMovementComponent* ProjectileMovement; 20 | 21 | public: 22 | AUnrealCPPProjectile(); 23 | 24 | /** called when projectile hits something */ 25 | UFUNCTION() 26 | void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit); 27 | 28 | /** Returns CollisionComp subobject **/ 29 | FORCEINLINE class USphereComponent* GetCollisionComp() const { return CollisionComp; } 30 | /** Returns ProjectileMovement subobject **/ 31 | FORCEINLINE class UProjectileMovementComponent* GetProjectileMovement() const { return ProjectileMovement; } 32 | }; 33 | --------------------------------------------------------------------------------