步枪支持瞄准镜变焦
2023年12月12日 2024年1月11日
Optical Zoom
绑定键位
API导航
APlayerController::PlayerCameraManager
管理摄像机
1/** Camera manager associated with this Player Controller. */ 2UPROPERTY(BlueprintReadOnly, Category=PlayerController) 3TObjectPtr<APlayerCameraManager> PlayerCameraManager;
APlayerCameraManager::GetFOVAngle
当前使用的FOV Field of View
1/** Returns the camera's current full FOV angle, in degrees. */ 2UFUNCTION(BlueprintCallable, Category = "Camera") 3virtual float GetFOVAngle() const;
APlayerCameraManager::SetFOV
设置FOV
1/** 2 * Locks the FOV to the given value. Unlock with UnlockFOV. 3 * @param NewFOV - New full FOV angle to use, in degrees. 4 */ 5virtual void SetFOV(float NewFOV);
InputComponent::BindAction<T>
若绑定键位模式为Action, 其默认回调函数形参列表为空
可以使用委托, 给出预设的其他形参
类似C++的bind
1/** 2* Binds a delegate function to an Action defined in the project settings. 3* Returned reference is only guaranteed to be valid until another action is bound. 4*/ 5template< class DelegateType, class UserClass, typename... VarTypes > 6FInputActionBinding& BindAction( const FName ActionName, const EInputEvent KeyEvent, UserClass* Object, typename DelegateType::template TMethodPtr< UserClass > Func, VarTypes... Vars ) 7{ 8 FInputActionBinding AB( ActionName, KeyEvent ); 9 AB.ActionDelegate.BindDelegate<DelegateType>(Object, Func, Vars...); 10 return AddActionBinding(MoveTemp(AB)); 11}
实现
武器基类
- 瞄准镜变焦为武器相关
- 步枪独有
public
ShootThemUp: Weapon/STUBaseWeapon.h
1virtual void Zoom(bool IsEnable) {}
步枪
- 添加属性: 瞄准镜放大系数
protected
ShootThemUp: Weapon/STURifleWeapon.h
1UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) 2float FOVZoomAngle = 50.0f;
- 添加属性: 瞄准镜默认放大系数
private
ShootThemUp: Weapon/STURifleWeapon.h
1float DefaultCameraFOV = 90.0f;
- 覆写基类Zoom函数
public
ShootThemUp: Weapon/STURifleWeapon.h
1virtual void Zoom(bool IsEnable) override;
ShootThemUp: Weapon/STURifleWeapon.cpp
1void ASTURifleWeapon::Zoom(bool IsEnable) 2{ 3 const auto PlayerController = Cast<APlayerController>(GetController()); 4 5 if (!PlayerController || !PlayerController->PlayerCameraManager) return; 6 7 if (IsEnable) 8 { 9 DefaultCameraFOV = PlayerController->PlayerCameraManager->GetFOVAngle(); 10 } 11 12 const auto NewFOV = IsEnable ? FOVZoomAngle : DefaultCameraFOV; 13 PlayerController->PlayerCameraManager->SetFOV(NewFOV); 14}
武器组件
作为键位绑定回调函数
public
ShootThemUp: Components/STUWeaponComponent.h
1void Zoom(bool Enabled);
ShootThemUp: Components/STUWeaponComponent.cpp
1void USTUWeaponComponent::Zoom(bool Enabled) 2{ 3 if (CurrentWeapon) 4 { 5 CurrentWeapon->Zoom(Enabled); 6 } 7}
游戏角色
按照常规思路, 会添加两个接口ZoomIn和ZoomOff, 调用武器组件的Zoom, 来填平形参
下面给出一个回避技巧, 达到相同的效果
ShootThemUp: Player/STUPlayerCharacter.cpp
1// SetupPlayerInputComponent 2 3DECLARE_DELEGATE_OneParam(FZoomInputSignature, bool); 4PlayerInputComponent->BindAction<FZoomInputSignature>("Zoom", IE_Pressed, WeaponComponent, &USTUWeaponComponent::Zoom, true); 5PlayerInputComponent->BindAction<FZoomInputSignature>("Zoom", IE_Released, WeaponComponent, &USTUWeaponComponent::Zoom, false);
问题
- 发射器不支持瞄准镜; 保持鼠标右键不释放, 切换武器, 摄像机视角为放大
ShootThemUp: Components/STUWeaponComponent.cpp
1// EquipWeapon 2 3if (CurrentWeapon) 4{ 5 CurrentWeapon->Zoom(false); 6 CurrentWeapon->FireStop(); 7 // ... 8}
- 游戏角色死亡后, 摄像机视角未解除放大
ShootThemUp: Player/STUBaseCharacter.cpp
1// OnDeath 2 3WeaponComponent->Zoom(false); 4WeaponComponent->FireStop();
和停止射击逻辑同时出现
若遇到卸载武器逻辑较为复杂的情况, 可以实现专门的UnEquip函数
查看
-
在关卡中添加BP_STUPlayerCharacter实例
-
调节Field of View参数: 数值变小, 起到放大效果