Compare commits

..

9 Commits

Author SHA1 Message Date
meishibiezb
bbe4ac4bc7 feat: 摄像头缩放 2026-03-15 19:16:24 +08:00
meishibiezb
877beaa3f5 feat: 摄像头移动 2026-03-15 19:06:35 +08:00
meishibiezb
d86b3f53a9 feat: 增加摄像机跟随 2026-03-15 18:42:17 +08:00
meishibiezb
402a0fed99 feat: 增加延迟生成 2026-03-15 17:57:21 +08:00
meishibiezb
746a85ae31 , 2026-03-15 17:24:20 +08:00
meishibiezb
166198c3e3 feat: 新建CameraPawn类 2026-03-15 12:43:39 +08:00
meishibiezb
5d2c3534cc refactor: 删除了一些空行 2026-03-15 12:23:01 +08:00
meishibiezb
d47d390dbb Merge branch 'dev'
feat: 增加移动系统
2026-03-15 12:18:29 +08:00
meishibiezb
8c91acd3d8 2026-03-15 01:56:27 +08:00
13 changed files with 244 additions and 42 deletions

View File

@@ -53,4 +53,3 @@ ManualIPAddress=
[/Script/UnrealBuildTool.UnrealBuildTool]
bAllowGitCommandlineIntegration=False

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,80 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "CameraPawn.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "InputActionValue.h"
void ACameraPawn::CameraZoom(const FInputActionValue& Value)
{
auto f = Value.Get<float>();
SpringArmComponent->TargetArmLength += f * 10.0f;
SpringArmComponent->TargetArmLength = FMath::Clamp(SpringArmComponent->TargetArmLength, 300.0f, 3000.0f);
}
void ACameraPawn::CameraRotate(const FInputActionValue& Value)
{
}
void ACameraPawn::CameraMove(const FInputActionValue& Value)
{
auto f2d = Value.Get<FVector2D>();
auto r = GetActorRightVector();
auto f = r.Cross(FVector::UpVector);
f.Normalize();
SetActorLocation(GetActorLocation() + f * f2d.X * 10.0f + r * f2d.Y * 10.0f);
}
// Sets default values
ACameraPawn::ACameraPawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// 初始化 SpringArmComponent 和 CameraComponent
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
SpringArmComponent->SetupAttachment(RootComponent);
SpringArmComponent->TargetArmLength = 1200.f;
SpringArmComponent->bUsePawnControlRotation = true;
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->SetupAttachment(SpringArmComponent, USpringArmComponent::SocketName);
// 设置弹簧臂碰撞属性
SpringArmComponent->bDoCollisionTest = false;
}
// Called when the game starts or when spawned
void ACameraPawn::BeginPlay()
{
Super::BeginPlay();
if (FollowTarget)
{
// 切换到当前摄像头
auto c = Cast<APlayerController>(FollowTarget->GetController());
if (c)
{
c->SetViewTarget(this);
}
// 附加到父物体
this->AttachToActor(FollowTarget, FAttachmentTransformRules::KeepWorldTransform);
}
}
// Called every frame
void ACameraPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ACameraPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}

View File

@@ -0,0 +1,43 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "CameraPawn.generated.h"
UCLASS()
class LONESE_API ACameraPawn : public APawn
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
class USpringArmComponent* SpringArmComponent;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
class UCameraComponent* CameraComponent;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
APawn* FollowTarget;
UFUNCTION(BlueprintCallable, Category = "Camera")
void CameraZoom(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable, Category = "Camera")
void CameraRotate(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable, Category = "Camera")
void CameraMove(const FInputActionValue& Value);
// Sets default values for this pawn's properties
ACameraPawn();
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
};

View File

@@ -7,6 +7,8 @@
#include "EnhancedInputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "CameraPawn.h"
void AMyCharacter::Move(const FInputActionValue& Value)
{
auto f2d = Value.Get<FVector2D>();
@@ -43,6 +45,30 @@ void AMyCharacter::StopCrouch(const FInputActionValue& Value)
UE_LOG(LogTemp, Warning, TEXT("Stop Crouch"));
}
void AMyCharacter::CameraMove(const FInputActionValue& Value)
{
if (CameraActor)
{
CameraActor->CameraMove(Value);
}
}
void AMyCharacter::CameraRotate(const FInputActionValue& Value)
{
if (CameraActor)
{
CameraActor->CameraRotate(Value);
}
}
void AMyCharacter::CameraZoom(const FInputActionValue& Value)
{
if (CameraActor)
{
CameraActor->CameraZoom(Value);
}
}
// Sets default values
AMyCharacter::AMyCharacter()
{
@@ -57,6 +83,29 @@ void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
auto w = GetWorld();
ACameraPawn* temp = nullptr;
if (w)
{
UClass* CameraClass;
if (CameraActorClass)
{
CameraClass = CameraActorClass;
}
else
{
CameraClass = ACameraPawn::StaticClass();
}
FTransform SpawnTransform(GetActorRotation(), GetActorLocation());
temp = w->SpawnActorDeferred<ACameraPawn>(CameraClass, SpawnTransform);
temp->SetActorRotation(FRotator(-60.0f, 0.0f, 0.0f));
temp->FollowTarget = this;
temp->FinishSpawning(SpawnTransform);
}
if (temp)
{
CameraActor = temp;
}
}
// Called every frame
@@ -96,6 +145,14 @@ void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCompone
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Started, this, &AMyCharacter::BeginCrouch);
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Completed, this, &AMyCharacter::StopCrouch);
}
if (CameraMoveAction)
{
EnhancedInputComponent->BindAction(CameraMoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::CameraMove);
}
if (CameraZoomAction)
{
EnhancedInputComponent->BindAction(CameraZoomAction, ETriggerEvent::Triggered, this, &AMyCharacter::CameraZoom);
}
}
}

View File

@@ -20,6 +20,13 @@ public:
class UInputAction* RunAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* CrouchAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* CameraMoveAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* CameraZoomAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
class TSubclassOf<class ACameraPawn> CameraActorClass;
UFUNCTION(BlueprintCallable, Category = "Move")
void Move(const FInputActionValue& Value);
@@ -52,4 +59,11 @@ protected:
private:
bool bIsRunning = false;
bool bIsCrouching = false;
UPROPERTY(VisibleInstanceOnly, Category = "Camera")
class ACameraPawn* CameraActor;
void CameraZoom(const FInputActionValue& Value);
void CameraRotate(const FInputActionValue& Value);
void CameraMove(const FInputActionValue& Value);
};

View File

@@ -7,7 +7,10 @@
{
"Name": "lonese",
"Type": "Runtime",
"LoadingPhase": "Default"
"LoadingPhase": "Default",
"AdditionalDependencies": [
"Engine"
]
}
],
"Plugins": [
@@ -24,11 +27,8 @@
"Enabled": true,
"SupportedTargetPlatforms": [
"Win64"
],
"MarketplaceURL": "com.epicgames.launcher://ue/marketplace/product/362651520df94e4fa65492dbcba44ae2"
}
]
}
],
"TargetPlatforms": [],
"AdditionalRootDirectories": [],
"AdditionalPluginDirectories": [],
"EpicSampleNameHash": ""
}