Add shooting from any direction
This commit is contained in:
parent
0bf9105fcd
commit
48519be2d7
@ -101,6 +101,10 @@ DoubleClickTime=0.200000
|
||||
+AxisMappings=(AxisName="MoveRight",Scale=-1.000000,Key=A)
|
||||
+AxisMappings=(AxisName="MoveForward",Scale=1.000000,Key=Gamepad_LeftY)
|
||||
+AxisMappings=(AxisName="MoveRight",Scale=1.000000,Key=Gamepad_LeftX)
|
||||
+AxisMappings=(AxisName="YawXUpdate",Scale=1.000000,Key=Gamepad_RightX)
|
||||
+AxisMappings=(AxisName="YawXUpdate",Scale=1.000000,Key=MouseX)
|
||||
+AxisMappings=(AxisName="YawYUpdate",Scale=-1.000000,Key=Gamepad_RightY)
|
||||
+AxisMappings=(AxisName="YawYUpdate",Scale=-1.000000,Key=MouseY)
|
||||
DefaultTouchInterface=/Engine/MobileResources/HUD/DefaultVirtualJoysticks.DefaultVirtualJoysticks
|
||||
-ConsoleKeys=Tilde
|
||||
+ConsoleKeys=Tilde
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -96,28 +96,39 @@ void ABasePawn::Tick(float DeltaTime)
|
||||
|
||||
void ABasePawn::handle_move_forward(float axis)
|
||||
{
|
||||
FRotator rotation = Controller->GetControlRotation();
|
||||
FRotator yaw(0.0f, rotation.Yaw, 0.0f);
|
||||
|
||||
FVector forward = FRotationMatrix(yaw).GetUnitAxis(EAxis::X);
|
||||
AddMovementInput(forward, axis);
|
||||
if (axis != 0)
|
||||
{
|
||||
FVector forward = FRotationMatrix(FRotator()).GetUnitAxis(EAxis::X);
|
||||
AddMovementInput(forward, axis);
|
||||
}
|
||||
}
|
||||
|
||||
void ABasePawn::handle_move_right(float axis)
|
||||
{
|
||||
FRotator rotation = Controller->GetControlRotation();
|
||||
FRotator yaw(0.0f, rotation.Yaw, 0.0f);
|
||||
if (axis != 0)
|
||||
{
|
||||
FVector right = FRotationMatrix(FRotator()).GetUnitAxis(EAxis::Y);
|
||||
AddMovementInput(right, axis);
|
||||
}
|
||||
}
|
||||
|
||||
FVector right = FRotationMatrix(yaw).GetUnitAxis(EAxis::Y);
|
||||
AddMovementInput(right, axis);
|
||||
void ABasePawn::update_yaw_x(float x_component)
|
||||
{
|
||||
yaw.x_component = x_component;
|
||||
}
|
||||
|
||||
void ABasePawn::update_yaw_y(float y_component)
|
||||
{
|
||||
yaw.y_component = y_component;
|
||||
}
|
||||
|
||||
void ABasePawn::shoot()
|
||||
{
|
||||
FVector player_location = player_mesh->GetComponentLocation();
|
||||
FRotator rotation = Controller->GetControlRotation();
|
||||
FRotator yaw(0.0f, rotation.Yaw, 0.0f);
|
||||
FVector forward = FRotationMatrix(yaw).GetUnitAxis(EAxis::X) * 100;
|
||||
const float y = yaw.calculate_yaw();
|
||||
LogInfo(FString::Printf(TEXT("Raw yaw: %f"), y));
|
||||
const FRotator rotator(0, y, 0);
|
||||
FVector forward = FRotationMatrix(rotator).GetUnitAxis(EAxis::X) * 100;
|
||||
|
||||
FVector spawn_location = player_location + forward;
|
||||
FActorSpawnParameters spawnParameters;
|
||||
@ -127,7 +138,7 @@ void ABasePawn::shoot()
|
||||
AActor* bullet = GetWorld()->SpawnActor<AActor>(
|
||||
bullet_actor_class,
|
||||
spawn_location,
|
||||
FRotator(0,0,0),
|
||||
rotator,
|
||||
spawnParameters
|
||||
);
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "CoreMinimal.h"
|
||||
#include "GameFramework/Pawn.h"
|
||||
#include "Components/BoxComponent.h"
|
||||
#include "Kismet/KismetMathLibrary.h"
|
||||
#include "BasePawn.generated.h"
|
||||
|
||||
class UFloatingPawnMovement;
|
||||
@ -24,9 +25,28 @@ public:
|
||||
virtual void Tick(float DeltaTime) override;
|
||||
virtual void handle_move_right(float axis);
|
||||
virtual void handle_move_forward(float axis);
|
||||
virtual void update_yaw_x(float x_component);
|
||||
virtual void update_yaw_y(float y_component);
|
||||
virtual void shoot();
|
||||
virtual void handle_death();
|
||||
protected:
|
||||
struct Yaw
|
||||
{
|
||||
float x_component;
|
||||
float y_component;
|
||||
|
||||
float calculate_yaw()
|
||||
{
|
||||
if (x_component < 0)
|
||||
{
|
||||
const float deg = UKismetMathLibrary::DegAtan2(-1 * x_component, y_component);
|
||||
return 360 - deg;
|
||||
}
|
||||
|
||||
return UKismetMathLibrary::DegAtan2(x_component, y_component);
|
||||
};
|
||||
};
|
||||
|
||||
// Called when the game starts or when spawned
|
||||
virtual void BeginPlay() override;
|
||||
|
||||
@ -85,4 +105,9 @@ protected:
|
||||
* Current health for character.
|
||||
*/
|
||||
int32 current_health;
|
||||
|
||||
/**
|
||||
* Last yaw.
|
||||
*/
|
||||
Yaw yaw;
|
||||
};
|
||||
|
@ -30,6 +30,8 @@ void APlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponen
|
||||
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
||||
PlayerInputComponent->BindAxis("MoveForward", this, &ABasePawn::handle_move_forward);
|
||||
PlayerInputComponent->BindAxis("MoveRight", this, &ABasePawn::handle_move_right);
|
||||
PlayerInputComponent->BindAxis("YawXUpdate", this, &ABasePawn::update_yaw_x);
|
||||
PlayerInputComponent->BindAxis("YawYUpdate", this, &ABasePawn::update_yaw_y);
|
||||
PlayerInputComponent->BindAction("Shoot", EInputEvent::IE_Pressed, this, &ABasePawn::shoot);
|
||||
PlayerInputComponent->BindAction("Boost", EInputEvent::IE_Pressed, this, &APlayerPawn::boost);
|
||||
PlayerInputComponent->BindAction("Boost", EInputEvent::IE_Released, this, &APlayerPawn::reset_boost);
|
||||
|
Loading…
Reference in New Issue
Block a user