I want to make the camera turn 45° degrees by pressing the “W”, the camera turns me but not work itself turn pawns. how to make and turned to the camera and a pawn? here is my code( my camera rotation in “CameraEffect”).where is my error?
AMyProject7Pawn::AMyProject7Pawn()
{
CreatePlane();
CreateSpringArm();
CreateCamera();
// Set handling parameters
Acceleration = 500.f;
TurnSpeed = 50.f;
MaxSpeed = 8000.f;
MinSpeed = 500.f;
CurrentForwardSpeed = 500.f;
}
void AMyProject7Pawn::CameraEffects()
{
FRotator BreakRotator = Controller->GetControlRotation();
FRotator MakeRotator;
MakeRotator.Roll = RollChooser;
MakeRotator.Pitch = FMath::Clamp(CurrentPitchSpeed*0.1, -45.0, 45.0) * -10;
MakeRotator.Yaw = 0;
SpringArm->SetRelativeRotation(MakeRotator, true);
SpringArm->TargetArmLength = CurrentForwardSpeed * 0.1 + 730;
}
void AMyProject7Pawn::Tick(float DeltaSeconds)
{
MoveShip(DeltaSeconds);
CameraAffects();
// Call any parent class Tick implementation
Super::Tick(DeltaSeconds);
}
void AMyProject7Pawn::NotifyHit(class UPrimitiveComponent* MyComp, class AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit)
{
Super::NotifyHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation, HitNormal, NormalImpulse, Hit);
// Deflect along the surface when we collide.
FRotator CurrentRotation = GetActorRotation(RootComponent);
SetActorRotation(FQuat::Slerp(CurrentRotation.Quaternion(), HitNormal.ToOrientationQuat(), 0.025f));
}
void AMyProject7Pawn::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
check(PlayerInputComponent);
// Bind our control axis' to callback functions
PlayerInputComponent->BindAxis("Thrust", this, &AMyProject7Pawn::ThrustInput);
PlayerInputComponent->BindAxis("MoveUp", this, &AMyProject7Pawn::MoveUpInput);
PlayerInputComponent->BindAxis("MoveRight", this, &AMyProject7Pawn::MoveRightInput);
PlayerInputComponent->BindAction("Camera", IE_Pressed, this, &AMyProject7Pawn::InputR);
}
void AMyProject7Pawn::CreatePlane()
{
// Structure to hold one-time initialization
struct FConstructorStatics
{
ConstructorHelpers::FObjectFinderOptional<USkeletalMesh> PlaneMesh;
FConstructorStatics()
: PlaneMesh(TEXT("SkeletalMesh'/Game/Flying/Meshes/1_non-triangle.1_non-triangle'"))
{
}
};
static FConstructorStatics ConstructorStatics;
// Create static mesh component
PlaneMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("PlaneMesh0"));
PlaneMesh->SetSkeletalMesh(ConstructorStatics.PlaneMesh.Get());
RootComponent = PlaneMesh;
}
void AMyProject7Pawn::CreateSpringArm()
{
// Create a spring arm component
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm0"));
SpringArm->SetupAttachment(RootComponent);
SpringArm->TargetArmLength = 700.0f; // The camera follows at this distance behind the character
SpringArm->SocketOffset = FVector(0.f, 0.f, 60.f);
SpringArm->bEnableCameraLag = false;
SpringArm->CameraLagSpeed = 15.f;
}
void AMyProject7Pawn::CreateCamera()
{
Camera = CreateDefaultSubobject<USpringArmComponent>(TEXT("Camera0"));
Camera->SetupAttachment(RootComponent);
Camera->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
Camera->bUsePawnControlRotation = true; // Rotate the arm based on the controller
// Create camera component
//Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera0"));
//Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
//Camera->bUsePawnControlRotation = false; // Don't rotate camera with controller
}
void AMyProject7Pawn::MoveShip(float DeltaTime)
{
FVector LocalOffSet = FVector(CurrentForwardSpeed * DeltaTime, 0.f, 0.f);
// Move plan forwards (with sweep so we stop when we collide with things)
AddActorLocalOffset(LocalOffSet, true);
// Calculate change in rotation this frame
FRotator DeltaRotation(0, 0, 0);
DeltaRotation.Roll = CurrentRollSpeed * DeltaTime;
DeltaRotation.Yaw = CurrentYawSpeed * DeltaTime;
DeltaRotation.Roll = CurrentRollSpeed * DeltaTime;
// Rotate plane
AddActorLocalRotation(DeltaRotation);
}
void AMyProject7Pawn::ThrustInput(float Val)
{
// Is there no input?
bool bHasInput = !FMath::IsNearlyEqual(Val, 0.f);
// If input is not held down, reduce speed
float CurrentAcc = bHasInput ? (Val * Acceleration) : (-0.5f * Acceleration);
// Calculate new speed
float NewForwardSpeed = CurrentForwardSpeed + (GetWorld()->GetDeltaSeconds() * CurrentAcc);
// Clamp between MinSpeed and MaxSpeed
CurrentForwardSpeed = FMath::Clamp(NewForwardSpeed, MinSpeed, MaxSpeed);
}
void AMyProject7Pawn::MoveUpInput(float Val)
{
// Target pitch speed is based in input
float TargetPitchSpeed = (Val * TurnSpeed * -1.f);
// When steering, we decrease pitch slightly
TargetPitchSpeed += (FMath::Abs(CurrentYawSpeed) * -0.2f);
// Smoothly interpolate to target pitch speed
CurrentPitchSpeed = FMath::FInterpTo(CurrentPitchSpeed, TargetPitchSpeed, GetWorld()->GetDeltaSeconds(), 2.f);
}
void AMyProject7Pawn::MoveRightInput(float Val)
{
// Target roll speed is based on input
float TargetRollSpeed = (Val * TurnSpeed * 2);
// Smoothly interpolate to target roll speed
CurrentRollSpeed = FMath::FInterpTo(CurrentRollSpeed, TargetRollSpeed, GetWorld()->GetDeltaSeconds(), 2.f);
}
void AMyProject7Pawn::InputR()
{
CameraRolling = !CameraRolling;
}