feat: 更新角色移动时旋转的功能

This commit is contained in:
meishibiezb
2026-03-17 11:33:45 +08:00
parent bdf3a1a8da
commit b8e1b5814f
3 changed files with 19 additions and 5 deletions

Binary file not shown.

View File

@@ -74,7 +74,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,19 @@
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 = FRotator(0, FMath::Atan2(f2d.Y, f2d.X) * 180.0f / PI, 0);
SetActorRotation(Rot);
}
} }
void AMyCharacter::BeginRun(const FInputActionValue& Value) void AMyCharacter::BeginRun(const FInputActionValue& Value)
@@ -84,6 +93,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