前面总结了官方教程
【UE4】蓝图转为C++官方教程部分笔记
这里我也按照我的理解,简单的将官方模板双摇杆射击游戏从蓝图转为C++供大家参考。
这种转换的方法并不一定很好,只是希望能够用上教程中的转换方法,巩固知识。
UFUNCTION(BlueprintCallable)
void FireShot(FVector Direction) ;
UPROPERTY(BlueprintReadWrite)
bool CanFire;
void ANewPawn::FireShot(FVector Direction)
{
if(!CanFire) return ;
if(Direction.Size() <= 0) return;
}
if(!CanFire) return ;
if(Direction.Size() <= 0) return;
FRotator ShipRotator = Direction.ToOrientationRotator() ;
FVector SpawnPoint = GetActorLocation()+Direction.ToOrientationRotator().RotateVector(GunOffset);
FTransform SpawnTransform = FTransform(ShipRotator, SpawnPoint, FVector(1.0f,1.0f,1.0f));
UFUNCTION(BlueprintCallable,BlueprintNativeEvent)
void FireShot(FVector Direction) ;
void ANewPawn::FireShot_Implementation(FVector Direction)
{
if(!CanFire) return ;
if(Direction.Size() <= 0) return;
FRotator ShipRotator = Direction.ToOrientationRotator() ;
FVector SpawnPoint = GetActorLocation()+Direction.ToOrientationRotator().RotateVector(GunOffset);
SpawnTransform = FTransform(ShipRotator, SpawnPoint, FVector(1.0f,1.0f,1.0f));
}
bool ANewPawn::FireShot_Implementation(FVector Direction)
{
if(!CanFire) return false;
if(Direction.Size() <= 0) return false;
FRotator ShipRotator = Direction.ToOrientationRotator() ;
FVector SpawnPoint = GetActorLocation()+Direction.ToOrientationRotator().RotateVector(GunOffset);
SpawnTransform = FTransform(ShipRotator, SpawnPoint, FVector(1.0f,1.0f,1.0f));
return true ;
}
UFUNCTION(BlueprintCallable,BlueprintImplementableEvent)
void HandleRotation(FRotator Rotation,FVector NewLocation) ;
void ANewPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector MoveVector = FVector(GetInputAxisValue(FName("MoveForward")),GetInputAxisValue(FName("MoveRight")),0.0f);
MoveVector = MoveVector.GetClampedToMaxSize(1.0f);
MoveVector = MovementSpeed * DeltaTime * MoveVector ;
if(MoveVector.Size() <= 0) return ;
FRotator XRotator = MoveVector.ToOrientationRotator();
//调用蓝图可实现事件HandleRotation
HandleRotation(XRotator,MoveVector);
//调用蓝图本地时间FireVector
FVector FireVector = FVector(GetInputAxisValue(FName("FireForward")),GetInputAxisValue(FName("FireRight")),0.0f);
FireShot(FireVector);
}
// Called to bind functionality to input
void ANewPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(FName("MoveForward"));
PlayerInputComponent->BindAxis(FName("MoveRight"));
PlayerInputComponent->BindAxis(FName("FireForward"));
PlayerInputComponent->BindAxis(FName("FireRight"));
//不执行这个绑定的话就无法获取输入
}