在诸如Visual Studio甚至只是记事本这样的文本编辑器中打开GravityController.h。删除其中的所有内容并将其替换为以下代码:#pragma once
#include "CoreMinimal.h"#include "GameFramework/PlayerController.h"#include "GravityController.generated.h"
/** * A Player Controller class which adds input-handling functionality for * CharacterMovementController's custom gravity mechanics. */UCLASS()class AGravityController : public APlayerController{ GENERATED_BODY()
public: virtual void UpdateRotation(float DeltaTime) override;
// Converts a rotation from world space to gravity relative space. UFUNCTION(BlueprintPure) static FRotator GetGravityRelativeRotation(FRotator Rotation, FVector GravityDirection);
// Converts a rotation from gravity relative space to world space. UFUNCTION(BlueprintPure) static FRotator GetGravityWorldRotation(FRotator Rotation, FVector GravityDirection);
private: FVector LastFrameGravity = FVector::ZeroVector;};现在打开GravityController.cpp,删除其中的所有内容并将其替换为以下代码:#include "GravityController.h"#include "GameFramework/Character.h"#include "GameFramework/CharacterMovementComponent.h"
void AGravityController::UpdateRotation(float DeltaTime){ FVector GravityDirection = FVector::DownVector; if (ACharacter* PlayerCharacter = Cast<ACharacter>(GetPawn())) { if (UCharacterMovementComponent* MoveComp = PlayerCharacter->GetCharacterMovement()) { GravityDirection = MoveComp->GetGravityDirection(); } }
// Get the current control rotation in world space FRotator ViewRotation = GetControlRotation();
// Add any rotation from the gravity changes, if any happened. // Delete this code block if you don't want the camera to automatically compensate for gravity rotation. if (!LastFrameGravity.Equals(FVector::ZeroVector)) { const FQuat DeltaGravityRotation = FQuat::FindBetweenNormals(LastFrameGravity, GravityDirection); const FQuat WarpedCameraRotation = DeltaGravityRotation * FQuat(ViewRotation);
ViewRotation = WarpedCameraRotation.Rotator(); } LastFrameGravity = GravityDirection;
// Convert the view rotation from world space to gravity relative space. // Now we can work with the rotation as if no custom gravity was affecting it. ViewRotation = GetGravityRelativeRotation(ViewRotation, GravityDirection);
// Calculate Delta to be applied on ViewRotation FRotator DeltaRot(RotationInput);
if (PlayerCameraManager) { ACharacter* PlayerCharacter = Cast<ACharacter>(GetPawn());
PlayerCameraManager->ProcessViewRotation(DeltaTime, ViewRotation, DeltaRot);
// Zero the roll of the camera as we always want it horizontal in relation to the gravity. ViewRotation.Roll = 0;
// Convert the rotation back to world space, and set it as the current control rotation. SetControlRotation(GetGravityWorldRotation(ViewRotation, GravityDirection)); }
APawn* const P = GetPawnOrSpectator(); if (P) { P->FaceRotation(ViewRotation, DeltaTime); }}
FRotator AGravityController::GetGravityRelativeRotation(FRotator Rotation, FVector GravityDirection){ if (!GravityDirection.Equals(FVector::DownVector)) { FQuat GravityRotation = FQuat::FindBetweenNormals(GravityDirection, FVector::DownVector); return (GravityRotation * Rotation.Quaternion()).Rotator(); }
return Rotation;}
FRotator AGravityController::GetGravityWorldRotation(FRotator Rotation, FVector GravityDirection){ if (!GravityDirection.Equals(FVector::DownVector)) { FQuat GravityRotation = FQuat::FindBetweenNormals(FVector::DownVector, GravityDirection); return (GravityRotation * Rotation.Quaternion()).Rotator(); }
return Rotation;}编译项目在此部分,您需要安装Visual Studio。如果您尚未安装,请查看本教程,了解如何执行此操作:设置Visual Studio | Epic开发者社区(https://dev.epicgames.com/docume ... ts-in-unreal-engine)。打开项目的Visual Studio解决方案文件。