PlayerCameraManagerClass = AMyPlayerCameraManager::StaticClass();
void AMyPlayerCameraManager::UpdateCamera(float DeltaTime)
{
Super::UpdateCamera(DeltaTime);
if (MyCharacter && MyCharacter->ActorHasTag(FName("MyCharacter")))
{
CustomCameraBehaivor(DeltaTime);
}
//Shake.ExecuteIfBound(this);
/*if (UMyWeaponSubsystem::GetMyWeaponSubsystem()->ShakeCamera)
{
StartCameraShake(MyCharacter->CameraShakeClass, UMyWeaponSubsystem::GetMyWeaponSubsystem()->ShakeScale);
}*/
}
void AMyPlayerCameraManager::CustomCameraBehaivor(float DeltaTime)
{
PivotValue = MyCharacter->GetActorTransform();//中心锚点位置
//设置过度到中心锚点的中间值,获得过度锚点
PivotTrasitionValue.SetLocation(CalculateTransitionValue_Location(PivotTrasitionValue.GetLocation(), PivotValue.GetLocation(), FVector(
GetCurveValue(FName("PivotLagSpeed_X")),
GetCurveValue(FName("PivotLagSpeed_Y")),
GetCurveValue(FName("PivotLagSpeed_Z"))
)));
PivotTrasitionValue.SetRotation(PivotValue.GetRotation());
//在过渡的锚点值上添加偏移值,获得偏移锚点
PivotOffsetValue.SetLocation(PivotTrasitionValue.GetLocation()+
UKismetMathLibrary::Multiply_VectorFloat(UKismetMathLibrary::GetForwardVector(PivotValue.GetRotation().Rotator()), GetCurveValue(FName("PivotOffset_X"))) +
UKismetMathLibrary::Multiply_VectorFloat(UKismetMathLibrary::GetRightVector(PivotValue.GetRotation().Rotator()), GetCurveValue(FName("PivotOffset_Y"))) +
UKismetMathLibrary::Multiply_VectorFloat(UKismetMathLibrary::GetUpVector(PivotValue.GetRotation().Rotator()), GetCurveValue(FName("PivotOffset_Z"))));
PivotOffsetValue.SetRotation(PivotValue.GetRotation());
//在偏移锚点值上添加相机偏移值,获得相机最终位置
CameraTargetValue.SetLocation(
FVector(PivotOffsetValue.GetLocation() +
UKismetMathLibrary::Multiply_VectorFloat(UKismetMathLibrary::GetForwardVector(PlayerController->GetControlRotation()), GetCurveValue(FName("CameraOffset_X"))) +
UKismetMathLibrary::Multiply_VectorFloat(UKismetMathLibrary::GetRightVector(PlayerController->GetControlRotation()), GetCurveValue(FName("CameraOffset_Y"))) +
UKismetMathLibrary::Multiply_VectorFloat(UKismetMathLibrary::GetUpVector(PlayerController->GetControlRotation()), GetCurveValue(FName("CameraOffset_Z")))
));
//给相机旋转进行插值变化
CameraTargetValue.SetRotation(UKismetMathLibrary::RInterpTo(
CameraTargetValue.GetRotation().Rotator(),
PlayerController->GetControlRotation(),
DeltaTime,
GetCurveValue(FName("RotationLagSpeed"))
).Quaternion());
//射线检测防止相机穿模
FVector start = MyCharacter->GetMesh()->GetSocketLocation(FName("TP_CameraTrace_R"));
FVector end = CameraTargetValue.GetLocation();
FHitResult Hit;
ETraceTypeQuery TraceType=UEngineTypes::ConvertToTraceType(ECC_Visibility);
UKismetSystemLibrary::SphereTraceSingle(GetWorld(), start, end, 10.f, TraceType, false, TArray<AActor*>(), EDrawDebugTrace::ForOneFrame, Hit, true);
if (Hit.bBlockingHit && !Hit.bStartPenetrating)
{
CameraTargetValue.SetLocation(CameraTargetValue.GetLocation() + Hit.Location - Hit.TraceEnd);
}
//应用最终数值到相机
FMinimalViewInfo&NewPOV= ViewTarget.POV;
NewPOV.Location = CameraTargetValue.GetLocation();
NewPOV.Rotation = CameraTargetValue.GetRotation().Rotator();
SetCameraCachePOV(NewPOV);//必须调用才能更改,ViewTarget.POV并不是最终的应用数据,即使用引用更改ViewTarget.POV也无效
//GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Blue, FString::Printf(TEXT("TransitionVlaueGetLocation: %s"), *TransitionVlaue.GetLocation().ToString()));
//GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Blue, FString::Printf(TEXT("TransitionVlaueGetRotation: %s"), *TransitionVlaue.GetRotation().ToString()));
//调试
UKismetSystemLibrary::DrawDebugSphere(GetWorld(), PivotValue.GetLocation(), 16, 8, FColor::Red);//中心锚点
UKismetSystemLibrary::DrawDebugSphere(GetWorld(), PivotTrasitionValue.GetLocation(), 16, 8, FColor::Blue);//过度锚点
UKismetSystemLibrary::DrawDebugSphere(GetWorld(), PivotOffsetValue.GetLocation(), 16, 8, FColor::Yellow);//偏移锚点
UKismetSystemLibrary::DrawDebugLine(GetWorld(), PivotValue.GetLocation(), PivotTrasitionValue.GetLocation(), FColor::Black);
UKismetSystemLibrary::DrawDebugLine(GetWorld(), PivotTrasitionValue.GetLocation(), PivotOffsetValue.GetLocation(), FColor::Black);
}