Add Camera Shake when player is damaged
This commit is contained in:
		
							parent
							
								
									c9c128ba44
								
							
						
					
					
						commit
						9ddc4c9ace
					
				
							
								
								
									
										
											BIN
										
									
								
								Content/Player/BP_PlayerCharacter.uasset
									 (Stored with Git LFS)
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Content/Player/BP_PlayerCharacter.uasset
									 (Stored with Git LFS)
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								Content/Player/C_Shake.uasset
									 (Stored with Git LFS)
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Content/Player/C_Shake.uasset
									 (Stored with Git LFS)
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| @ -29,6 +29,16 @@ APlayerCharacter::APlayerCharacter() | ||||
| 	HealthBarWidgetComponent->SetTwoSided(true); | ||||
| 	HealthBarWidgetComponent->SetBackgroundColor(FLinearColor(1, 1, 1, 0)); | ||||
| 	HealthBarWidgetComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision); | ||||
| 
 | ||||
| 	CameraShakeTimelineComponent = CreateDefaultSubobject<UTimelineComponent>(TEXT("Camera Shake Timeline Component")); | ||||
| 	CameraShakeTimelineComponent->SetDirectionPropertyName(FName("TimelineDirection")); | ||||
| 	CameraShakeTimelineComponent->SetLooping(false); | ||||
| 	CameraShakeTimelineComponent->SetTimelineLength(0.5f); | ||||
| 	CameraShakeTimelineComponent->SetTimelineLengthMode(TL_TimelineLength); | ||||
| 	CameraShakeTimelineComponent->SetPlaybackPosition(0.0f, false); | ||||
| 
 | ||||
| 	onTimelineCallback.BindUFunction(this, FName(TEXT("CameraShakeTimelineCallback"))); | ||||
| 	onTimelineFinishedCallback.BindUFunction(this, FName(TEXT("CameraShakeTimelineFinishedCallback"))); | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::BeginPlay() | ||||
| @ -37,6 +47,14 @@ void APlayerCharacter::BeginPlay() | ||||
| 
 | ||||
| 	GetHealthComponent()->OnDamaged.AddDynamic(this, &APlayerCharacter::OnDamaged); | ||||
| 	GetHealthComponent()->OnDeath.AddDynamic(this, &APlayerCharacter::OnDeath); | ||||
| 
 | ||||
| 	if (CameraShakeCurve != nullptr) | ||||
| 	{ | ||||
| 		CameraShakeTimelineComponent->AddInterpFloat(CameraShakeCurve, onTimelineCallback); | ||||
| 		CameraShakeTimelineComponent->SetTimelineFinishedFunc(onTimelineFinishedCallback); | ||||
| 	} | ||||
| 
 | ||||
| 	PlayerCameraManager = GetWorld()->GetFirstPlayerController()->PlayerCameraManager; | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::Tick(float DeltaTime) | ||||
| @ -78,11 +96,7 @@ void APlayerCharacter::OnDamaged(FDamageInfo damageInfo) | ||||
| 		UGameplayStatics::PlaySoundAtLocation(GetWorld(), OnDamagedSound, GetActorLocation()); | ||||
| 	} | ||||
| 
 | ||||
| 	APlayerController* playerController = UGameplayStatics::GetPlayerController(this, 0); | ||||
| 	if (playerController && CameraShake) | ||||
| 	{ | ||||
| 		playerController->ClientStartCameraShake(CameraShake); | ||||
| 	} | ||||
| 	CameraShakeTimelineComponent->PlayFromStart(); | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::OnDeath(FDamageInfo damageInfo) | ||||
| @ -94,3 +108,17 @@ void APlayerCharacter::OnDeath(FDamageInfo damageInfo) | ||||
| 
 | ||||
| 	// TODO: End the game
 | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::CameraShakeTimelineCallback(float val) | ||||
| { | ||||
| 	auto PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0); | ||||
| 	auto cameraActor = PlayerController->GetViewTarget(); | ||||
| 	cameraActor->SetActorLocation(FVector(FMath::RandRange(0.0f, CameraShakeStrength) * val, FMath::RandRange(0.0f, CameraShakeStrength) * val, 0.0f)); | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::CameraShakeTimelineFinishedCallback() | ||||
| { | ||||
| 	auto PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0); | ||||
| 	auto cameraActor = PlayerController->GetViewTarget(); | ||||
| 	cameraActor->SetActorLocation(FVector::ZeroVector); | ||||
| } | ||||
|  | ||||
| @ -4,8 +4,10 @@ | ||||
| 
 | ||||
| #include "CoreMinimal.h" | ||||
| #include "VampireCharacter.h" | ||||
| #include "Components/TimelineComponent.h" | ||||
| #include "PlayerCharacter.generated.h" | ||||
| 
 | ||||
| struct FDamageInfo; | ||||
| struct FInputActionInstance; | ||||
| class UWidgetComponent; | ||||
| class UWeaponInventoryComponent; | ||||
| @ -32,11 +34,25 @@ public: | ||||
| 	UPROPERTY(EditAnywhere) | ||||
| 	UWidgetComponent* HealthBarWidgetComponent; | ||||
| 
 | ||||
| 	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly) | ||||
| 	TObjectPtr<UTimelineComponent> CameraShakeTimelineComponent = nullptr; | ||||
| 	 | ||||
| 	UPROPERTY(EditAnywhere) | ||||
| 	TSubclassOf<UCameraShakeBase> CameraShake; | ||||
| 	TObjectPtr<UCurveFloat> CameraShakeCurve; | ||||
| 
 | ||||
| 	UPROPERTY(EditAnywhere) | ||||
| 	float CameraShakeStrength = 100.0f; | ||||
| 
 | ||||
| private: | ||||
| 	 | ||||
| 	TObjectPtr<APlayerCameraManager> PlayerCameraManager = nullptr; | ||||
| 	 | ||||
| 	APlayerCharacter(); | ||||
| 
 | ||||
| private: | ||||
| 	FOnTimelineFloat onTimelineCallback; | ||||
| 	FOnTimelineEventStatic onTimelineFinishedCallback; | ||||
| 
 | ||||
| protected: | ||||
| 	virtual void BeginPlay() override; | ||||
| 
 | ||||
| @ -53,4 +69,10 @@ private: | ||||
| 
 | ||||
| 	UFUNCTION() | ||||
| 	virtual void OnDeath(FDamageInfo damageInfo); | ||||
| 
 | ||||
| 	UFUNCTION() | ||||
| 	void CameraShakeTimelineCallback(float val); | ||||
| 
 | ||||
| 	UFUNCTION() | ||||
| 	void CameraShakeTimelineFinishedCallback(); | ||||
| }; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user