一,子弹类型
// .h文件
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/SphereComponent.h"
#include "GameFrameWork/ProjectileMovementComponent.h"
#include "MyActor.generated.h"
// ...
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UStaticMeshComponent> Sphere;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<USphereComponent> SphereCollision;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UProjectileMovementComponent> ProjectileMovement;
// .cpp文件
AMyActor::AMyActor() {
PrimaryActorTick.bCanEverTick = true;
Sphere = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Sphere"));
SphereCollision = CreateDefaultSubobject<USphereComponent>(TEXT("SphereCollision"));
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticSphereMesh(TEXT("/Engine/BasicShapes/Sphere.Sphere"));
RootComponent = Sphere;
SphereCollision->SetupAttachment(RootComponent);
ProjectileMovement->SetUpdatedComponent(RootComponent);
Sphere->SetStaticMesh(StaticSphereMesh.Object);
ProjectileMovement->InitialSpeed = 500.0;
ProjectileMovement->MaxSpeed = 2000.0;
}
二,带有增强输入的角色类型
// .h文件
#include "CoreMinimal.h"
#include "InputActionValue.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Character.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "MyCharacter.generated.h"
class UInputAction;
class UInputMappingContext;
// ...
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<USpringArmComponent> SpringArm;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UCameraComponent> Camera;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UInputMappingContext> DefaultMappingContext;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UInputAction> MoveAction;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UInputAction> LookAction;
public:
void Move(const FInputActionValue& value);
void Look(const FInputActionValue& value);
// ...
// .cpp文件
AMyCharacter::AMyCharacter()
{
PrimaryActorTick.bCanEverTick = true;
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
SpringArm->TargetArmLength = 400.0f;
SpringArm->bUsePawnControlRotation = true;
SpringArm->SetupAttachment(RootComponent);
Camera->SetupAttachment(SpringArm);
//控制器只影响相机不影响角色
bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
}
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
if (auto PlayerController = Cast<APlayerController>(Controller))
if (auto Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (auto EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMyCharacter::Look);
}
}
void AMyCharacter::Move(const FInputActionValue& value)
{
FVector2D MovementVector = value.Get<FVector2D>();
if (Controller)
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector FowardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(FowardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
// 5.7使用IMC_Default 和 IMC_MouseLook 将按键和鼠标操作分开
// IMC_Default → 按键操作(移动、跳跃、冲刺、交互等)
// IMC_MouseLook → 鼠标操作(视角旋转)
void AMyCharacter::Look(const FInputActionValue& value)
{
FVector2D LookAxisVector = value.Get<FVector2D>();
if (Controller)
{
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}