This commit is contained in:
TNT
2026-03-17 20:56:06 +08:00
5 changed files with 42 additions and 6 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -17,8 +17,15 @@ void ACameraPawn::CameraZoom(const FInputActionValue& Value)
void ACameraPawn::CameraRotate(const FInputActionValue& Value) void ACameraPawn::CameraRotate(const FInputActionValue& Value)
{ {
auto f = Value.Get<float>(); auto f = Value.Get<float>();
FRotator r = FRotator(0.0f, f * RotateSpeed, 0.0f); FRotator r = FRotator(0.0f, -f * RotateSpeed, 0.0f);
AddActorWorldRotation(r); AddActorWorldRotation(r);
// 同时旋转控制器
auto c = Cast<APlayerController>(FollowTarget->GetController());
if (c)
{
c->SetControlRotation(GetActorRotation());
}
} }
void ACameraPawn::CameraMove(const FInputActionValue& Value) void ACameraPawn::CameraMove(const FInputActionValue& Value)
@@ -38,6 +45,13 @@ void ACameraPawn::CameraReset(const FInputActionValue& Value)
SpringArmComponent->TargetArmLength = InitialArmLength; SpringArmComponent->TargetArmLength = InitialArmLength;
SetActorRotation(InitialRotation); SetActorRotation(InitialRotation);
SetActorRelativeLocation(FVector::ZeroVector); SetActorRelativeLocation(FVector::ZeroVector);
// 同时旋转控制器
auto c = Cast<APlayerController>(FollowTarget->GetController());
if (c)
{
c->SetControlRotation(GetActorRotation());
}
} }
// Sets default values // Sets default values
@@ -74,7 +88,7 @@ void ACameraPawn::BeginPlay()
// 附加到父物体 // 附加到父物体
this->AttachToActor(FollowTarget, FAttachmentTransformRules::KeepWorldTransform); this->AttachToActor(FollowTarget, FAttachmentTransformRules::KeepWorldTransform);
this->GetRootComponent()->SetAbsolute(false, true, true);
} }
// 设置自身旋转 // 设置自身旋转

View File

@@ -12,10 +12,21 @@
void AMyCharacter::Move(const FInputActionValue& Value) void AMyCharacter::Move(const FInputActionValue& Value)
{ {
auto f2d = Value.Get<FVector2D>(); auto f2d = Value.Get<FVector2D>();
auto f = GetActorForwardVector();
auto r = GetActorRightVector(); // 移动
auto f = FRotator(0, GetControlRotation().Yaw, 0).Vector();
auto r = -f.Cross(FVector::UpVector);
AddMovementInput(f * f2d.X); AddMovementInput(f * f2d.X);
AddMovementInput(r * f2d.Y); AddMovementInput(r * f2d.Y);
// 转向
if (f2d.SizeSquared() > 0.01f)
{
FRotator Rot = FVector(f2d.X, f2d.Y, 0).Rotation() + FRotator(0, GetControlRotation().Yaw, 0);
float InterpSpeed = 10.0f;
FRotator NewRotation = FMath::RInterpTo(GetActorRotation(), Rot, GetWorld()->GetDeltaSeconds(), InterpSpeed);
SetActorRotation(NewRotation);
}
} }
void AMyCharacter::BeginRun(const FInputActionValue& Value) void AMyCharacter::BeginRun(const FInputActionValue& Value)
@@ -84,6 +95,11 @@ AMyCharacter::AMyCharacter()
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
GetCharacterMovement()->GetNavAgentPropertiesRef().bCanCrouch = true; GetCharacterMovement()->GetNavAgentPropertiesRef().bCanCrouch = true;
// 禁用控制器旋转
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
} }
// Called when the game starts or when spawned // Called when the game starts or when spawned