修改船的移动逻辑,关闭摄像机的额碰撞检测
This commit is contained in:
parent
2d3c11663f
commit
b783586e79
Binary file not shown.
Binary file not shown.
@ -27,7 +27,11 @@ AShipbase::AShipbase()
|
|||||||
bUseControllerRotationRoll = false;
|
bUseControllerRotationRoll = false;
|
||||||
|
|
||||||
//默认movement
|
//默认movement
|
||||||
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
|
//GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
|
||||||
|
//修改移动模式
|
||||||
|
GetCharacterMovement()->bOrientRotationToMovement = false; // Don't auto-orient to movement direction
|
||||||
|
GetCharacterMovement()->bUseControllerDesiredRotation = true; // Use controller rotation for turning
|
||||||
|
|
||||||
GetCharacterMovement()->bUseSeparateBrakingFriction = true;
|
GetCharacterMovement()->bUseSeparateBrakingFriction = true;
|
||||||
GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate
|
GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate
|
||||||
GetCharacterMovement()->MaxWalkSpeed = 500.f;
|
GetCharacterMovement()->MaxWalkSpeed = 500.f;
|
||||||
@ -46,7 +50,7 @@ AShipbase::AShipbase()
|
|||||||
CameraBoom->SetupAttachment(RootComponent);
|
CameraBoom->SetupAttachment(RootComponent);
|
||||||
CameraBoom->TargetArmLength = 400.0f; // The camera follows at this distance behind the character
|
CameraBoom->TargetArmLength = 400.0f; // The camera follows at this distance behind the character
|
||||||
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
|
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
|
||||||
|
CameraBoom->bDoCollisionTest = false; //关闭碰撞遮挡检测
|
||||||
//camera
|
//camera
|
||||||
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
|
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
|
||||||
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
|
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
|
||||||
@ -134,21 +138,64 @@ void AShipbase::Move(const FInputActionValue& Value)
|
|||||||
// input is a Vector2D
|
// input is a Vector2D
|
||||||
FVector2D MovementVector = Value.Get<FVector2D>();
|
FVector2D MovementVector = Value.Get<FVector2D>();
|
||||||
|
|
||||||
|
// if (Controller != nullptr)
|
||||||
|
// {
|
||||||
|
// // find out which way is forward
|
||||||
|
// const FRotator Rotation = Controller->GetControlRotation();
|
||||||
|
// const FRotator YawRotation(0, Rotation.Yaw, 0);
|
||||||
|
//
|
||||||
|
// // get forward vector
|
||||||
|
// const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
|
||||||
|
//
|
||||||
|
// // get right vector
|
||||||
|
// const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
|
||||||
|
//
|
||||||
|
// // add movement
|
||||||
|
// AddMovementInput(ForwardDirection, MovementVector.Y);
|
||||||
|
// AddMovementInput(RightDirection, MovementVector.X);
|
||||||
|
// }
|
||||||
|
//修改移动转向逻辑
|
||||||
if (Controller != nullptr)
|
if (Controller != nullptr)
|
||||||
{
|
{
|
||||||
// find out which way is forward
|
// Store input values
|
||||||
const FRotator Rotation = Controller->GetControlRotation();
|
float ForwardInput = MovementVector.Y; // W/S keys for forward/backward
|
||||||
const FRotator YawRotation(0, Rotation.Yaw, 0);
|
float TurnInput = MovementVector.X; // A/D keys for turning
|
||||||
|
|
||||||
// get forward vector
|
// Only move forward/backward along character's current facing direction
|
||||||
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
|
if (FMath::Abs(ForwardInput) > 0.1f)
|
||||||
|
{
|
||||||
|
// Get character's current forward direction
|
||||||
|
const FVector ForwardDirection = GetActorForwardVector();
|
||||||
|
|
||||||
// get right vector
|
// Apply forward/backward movement
|
||||||
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
|
AddMovementInput(ForwardDirection, ForwardInput);
|
||||||
|
|
||||||
// add movement
|
// Apply turning only when moving (like a car)
|
||||||
AddMovementInput(ForwardDirection, MovementVector.Y);
|
if (FMath::Abs(TurnInput) > 0.1f)
|
||||||
AddMovementInput(RightDirection, MovementVector.X);
|
{
|
||||||
|
// 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
|
||||||
|
FRotator NewRotation = GetActorRotation();
|
||||||
|
NewRotation.Yaw += TurnRate;
|
||||||
|
SetActorRotation(NewRotation);
|
||||||
|
|
||||||
|
// Also update controller rotation for camera
|
||||||
|
if (APlayerController* PC = Cast<APlayerController>(Controller))
|
||||||
|
{
|
||||||
|
FRotator ControllerRotation = PC->GetControlRotation();
|
||||||
|
ControllerRotation.Yaw += TurnRate;
|
||||||
|
PC->SetControlRotation(ControllerRotation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user