更新小船操作逻辑

This commit is contained in:
997146918 2025-08-04 17:29:55 +08:00
parent 73bda7f568
commit c9434d19a3
6 changed files with 3264 additions and 43 deletions

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "ProjectFish/Components/ShipSpringArmComponent.h"
@ -65,7 +66,8 @@ AShipbase::AShipbase()
void AShipbase::BeginPlay()
{
Super::BeginPlay();
MoveForward = GetActorForwardVector();
MoveRight = GetActorRightVector();
}
// Called every frame
@ -154,48 +156,54 @@ void AShipbase::Move(const FInputActionValue& Value)
// AddMovementInput(ForwardDirection, MovementVector.Y);
// AddMovementInput(RightDirection, MovementVector.X);
// }
//修改移动转向逻辑
if (Controller != nullptr)
{
// Store input values
float ForwardInput = MovementVector.Y; // W/S keys for forward/backward
float TurnInput = MovementVector.X; // A/D keys for turning
// Only move forward/backward along character's current facing direction
if (FMath::Abs(ForwardInput) > 0.1f)
{
// Get character's current forward direction
const FVector ForwardDirection = GetActorForwardVector();
// Apply forward/backward movement
AddMovementInput(ForwardDirection, ForwardInput);
// Apply turning only when moving (like a car)
if (FMath::Abs(TurnInput) > 0.1f)
{
// Calculate turn rate based on movement speed and turn input
float TurnRate = TurnInput * ShipData.TurnSensitivity * GetWorld()->GetDeltaSeconds();
// For backward movement, invert steering (like a real car)
if (ForwardInput < 0.0f)
{
TurnRate *= -1.0f;
}
// Apply rotation to the character only, not the controller
FRotator NewRotation = GetActorRotation();
NewRotation.Yaw += TurnRate;
SetActorRotation(NewRotation);
// // Also update controller rotation for camera
// if (APlayerController* PC = Cast<APlayerController>(Controller))
//第二版修改移动转向逻辑
// if (Controller != nullptr)
// {
// FRotator ControllerRotation = PC->GetControlRotation();
// ControllerRotation.Yaw += TurnRate;
// PC->SetControlRotation(ControllerRotation);
// // Store input values
// float ForwardInput = MovementVector.Y; // W/S keys for forward/backward
// float TurnInput = MovementVector.X; // A/D keys for turning
//
// // Only move forward/backward along character's current facing direction
// if (FMath::Abs(ForwardInput) > 0.1f)
// {
// // Get character's current forward direction
// const FVector ForwardDirection = GetActorForwardVector();
//
// // Apply forward/backward movement
// AddMovementInput(ForwardDirection, ForwardInput);
//
// // Apply turning only when moving (like a car)
// if (FMath::Abs(TurnInput) > 0.1f)
// {
// // Calculate turn rate based on movement speed and turn input
// float TurnRate = TurnInput * ShipData.TurnSensitivity * GetWorld()->GetDeltaSeconds();
//
// // For backward movement, invert steering (like a real car)
// if (ForwardInput < 0.0f)
// {
// TurnRate *= -1.0f;
// }
}
}
}
//
// // Apply rotation to the character only, not the controller
// FRotator NewRotation = GetActorRotation();
// NewRotation.Yaw += TurnRate;
// SetActorRotation(NewRotation);
// }
// }
// }
//第三版移动逻辑WASD控制转向和移动
FVector TargetDirec = (MovementVector.Y * MoveForward + MovementVector.X * MoveRight).GetSafeNormal();
FRotator TargetRot = FRotationMatrix::MakeFromX(TargetDirec).Rotator();
FRotator CurrentRot = GetActorRotation();
FRotator NewRotator = FMath::RInterpTo(CurrentRot, TargetRot, GetWorld()->GetDeltaSeconds(), ShipData.TurnSensitivity);
SetActorRotation(NewRotator);
AddMovementInput(GetActorForwardVector(), FMath::Max(FMath::Abs(MovementVector.Y ), FMath::Abs(MovementVector.X)));
}
void AShipbase::Look(const FInputActionValue& Value)

View File

@ -52,6 +52,10 @@ protected:
void ApplyMovementSettings();
void ApplyCameraSettings();
private:
FVector MoveForward;
FVector MoveRight;
protected:
UPROPERTY(EditAnywhere,BlueprintReadOnly, Category= "Config", meta = (AllowPrivateAccess = "true"))
FShipDataConfig ShipData;