UE5 战斗 / 射击
UE5 第三人称射击demo
练习关卡搭建、材质、灯光、角色移动与基础交互,并记录每次迭代中的视觉与性能取舍。
- UE5
- Blueprint
- Level Design
- C++
- Git + LFS
ShootCharacter
ShootCharacter 代码c++
// Fill out your copyright notice in the Description page of Project Settings.
#include "ShootCharacter.h"
#include "Gun.h"
#include "SimpleShooterGameModeBase.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "Components/SkinnedMeshComponent.h"
#include "Engine/World.h"
// Sets default values
AShootCharacter::AShootCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AShootCharacter::BeginPlay()
{
Super::BeginPlay();
Health = MaxHealth;
Gun = GetWorld()->SpawnActor<AGun>(GunClass);
GetMesh()->HideBoneByName(TEXT("weapon_r"), EPhysBodyOp::PBO_None);
Gun->AttachToComponent(GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocket"));
Gun->SetOwner(this);
}
bool AShootCharacter::IsDead() const
{
return Health <= 0;
}
float AShootCharacter::GetHealthPercent() const
{
return Health / MaxHealth;
}
// Called every frame
void AShootCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AShootCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AShootCharacter::MoveForward);
PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AShootCharacter::MoveRight);
PlayerInputComponent->BindAxis(TEXT("LookUp"), this, &APawn::AddControllerPitchInput);
PlayerInputComponent->BindAxis(TEXT("LookRight"), this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis(TEXT("LookUpRate"), this, &AShootCharacter::LookUpRate);
PlayerInputComponent->BindAxis(TEXT("LookRightRate"), this, &AShootCharacter::LookRightRate);
PlayerInputComponent->BindAction(TEXT("Jump"), IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction(TEXT("Shoot"), IE_Pressed, this, &AShootCharacter::Shoot);
}
float AShootCharacter::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent,
class AController* EventInstigator, AActor* DamageCauser)
{
float DamageToApply = Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
DamageToApply = FMath::Min(Health, DamageToApply);
Health -= DamageToApply;
UE_LOG(LogTemp, Warning, TEXT("Health left: %f"), Health);
if (IsDead())
{
ASimpleShooterGameModeBase* GameMode = GetWorld()->GetAuthGameMode<ASimpleShooterGameModeBase>();
if (GameMode)
GameMode->PawnKilled(this);
DetachFromControllerPendingDestroy();
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
return DamageToApply;
}
void AShootCharacter::MoveForward(float AxisValue)
{
AddMovementInput(GetActorForwardVector(), AxisValue);
}
void AShootCharacter::MoveRight(float AxisValue)
{
AddMovementInput(GetActorRightVector(), AxisValue);
}
void AShootCharacter::LookUpRate(float AxisValue)
{
AddControllerPitchInput(AxisValue * RotationRate * GetWorld()->GetDeltaSeconds());
}
void AShootCharacter::LookRightRate(float AxisValue)
{
AddControllerYawInput(AxisValue * RotationRate * GetWorld()->GetDeltaSeconds());
}
void AShootCharacter::Shoot()
{
Gun->PullTrigger();
}