Fix rotations so character faces their previous direction when switching grav direction
This commit is contained in:
parent
bd20b78a3b
commit
8ac4271602
BIN
GravityStomp/Content/Characters/BP_GSCharacter.uasset
(Stored with Git LFS)
BIN
GravityStomp/Content/Characters/BP_GSCharacter.uasset
(Stored with Git LFS)
Binary file not shown.
@ -103,5 +103,20 @@ void AGSCharacter::ChangeGravityDirection(const FInputActionValue& Value)
|
||||
FVector2D GravityDirection = Value.Get<FVector2D>();
|
||||
FVector NewCharacterUpDirection(0.0f, -GravityDirection.X, -GravityDirection.Y);
|
||||
|
||||
FRotator CharRot = GetActorRotation();
|
||||
if(GetCharacterMovement<UGSCharacterMovementComponent>()->IsCharacterUpAlignedToWorldUp())
|
||||
{
|
||||
CharRot = FRotator(0.0f, CharRot.Yaw, 0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
CharRot = FRotator(CharRot.Pitch, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
float NewRoll = GetCharacterMovement<UGSCharacterMovementComponent>()->GetRollFromCharacterUpDir(NewCharacterUpDirection);
|
||||
FRotator RollRot = UKismetMathLibrary::RotatorFromAxisAndAngle(FVector::ForwardVector, NewRoll);
|
||||
|
||||
SetActorRotation(UKismetMathLibrary::ComposeRotators(RollRot, CharRot));
|
||||
|
||||
GetCharacterMovement<UGSCharacterMovementComponent>()->SetCharacterUpDirection(NewCharacterUpDirection);
|
||||
}
|
||||
|
@ -658,6 +658,25 @@ bool UGSCharacterMovementComponent::IsCharacterUpAlignedToWorldUp() const
|
||||
return FMath::Abs(CharacterUpDirection.Dot(FVector::UpVector)) > UE_KINDA_SMALL_NUMBER;
|
||||
}
|
||||
|
||||
float UGSCharacterMovementComponent::GetRollFromCharacterUpDir(const FVector& UpDir) const
|
||||
{
|
||||
float Rot = 0.0f;
|
||||
if(CharacterUpDirection.Dot(FVector::DownVector) > UE_KINDA_SMALL_NUMBER)
|
||||
{
|
||||
Rot = 180.0f;
|
||||
}
|
||||
else if (CharacterUpDirection.Dot(FVector::RightVector) > UE_KINDA_SMALL_NUMBER)
|
||||
{
|
||||
Rot = 90.0f;
|
||||
}
|
||||
else if (CharacterUpDirection.Dot(FVector::LeftVector) > UE_KINDA_SMALL_NUMBER)
|
||||
{
|
||||
Rot = 270.0f;
|
||||
}
|
||||
|
||||
return Rot;
|
||||
}
|
||||
|
||||
|
||||
void UGSCharacterMovementComponent::SetCharacterUpDirection(FVector NewUpDirection)
|
||||
{
|
||||
@ -842,21 +861,7 @@ void UGSCharacterMovementComponent::PhysicsRotation(float DeltaTime)
|
||||
|
||||
DesiredRotation.Pitch = IsCharacterUpAlignedToWorldUp() ? 0.f : FRotator::NormalizeAxis(DesiredRotation.Pitch);
|
||||
DesiredRotation.Yaw = IsCharacterUpAlignedToWorldUp() ? FRotator::NormalizeAxis(DesiredRotation.Yaw) : 0.f;
|
||||
|
||||
float Rot = 0.0f;
|
||||
if(CharacterUpDirection.Dot(FVector::DownVector) > UE_KINDA_SMALL_NUMBER)
|
||||
{
|
||||
Rot = 180.0f;
|
||||
}
|
||||
else if (CharacterUpDirection.Dot(FVector::RightVector) > UE_KINDA_SMALL_NUMBER)
|
||||
{
|
||||
Rot = 90.0f;
|
||||
}
|
||||
else if (CharacterUpDirection.Dot(FVector::LeftVector) > UE_KINDA_SMALL_NUMBER)
|
||||
{
|
||||
Rot = 270.0f;
|
||||
}
|
||||
DesiredRotation.Roll = Rot;
|
||||
DesiredRotation.Roll = GetRollFromCharacterUpDir(CharacterUpDirection);
|
||||
|
||||
DebugDesiredRotation = FString::Printf(TEXT("Desired Rotation after keeping it vertical: %s"), *DesiredRotation.ToString());
|
||||
GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Cyan, DebugDesiredRotation);
|
||||
|
@ -19,9 +19,18 @@ class GRAVITYSTOMPGAME_API UGSCharacterMovementComponent : public UCharacterMove
|
||||
public:
|
||||
UGSCharacterMovementComponent();
|
||||
|
||||
UFUNCTION(BlueprintPure)
|
||||
FORCEINLINE FVector GetCharacterUpDirection() const { return CharacterUpDirection; }
|
||||
|
||||
UFUNCTION(BlueprintCallable)
|
||||
void SetCharacterUpDirection(FVector NewUpDirection);
|
||||
|
||||
UFUNCTION(BlueprintPure)
|
||||
float GetRollFromCharacterUpDir(const FVector& UpDir) const;
|
||||
|
||||
UFUNCTION(BlueprintPure)
|
||||
FORCEINLINE bool IsCharacterUpAlignedToWorldUp() const;
|
||||
|
||||
// UCharacterMovementComponent interface
|
||||
|
||||
virtual FVector ConstrainInputAcceleration(const FVector& InputAcceleration) const override;
|
||||
@ -59,7 +68,4 @@ public:
|
||||
|
||||
private:
|
||||
FVector CharacterUpDirection = FVector::UpVector;
|
||||
|
||||
UFUNCTION(BlueprintPure)
|
||||
FORCEINLINE bool IsCharacterUpAlignedToWorldUp() const;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user