Skip to content

Commit

Permalink
#30 Refactored Character Rotation Lag
Browse files Browse the repository at this point in the history
  • Loading branch information
xthebat committed Oct 30, 2023
1 parent 4130a3b commit 92896ca
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 83 deletions.
Binary file modified Content/Blueprints/Character/BP_Cloud9Character.uasset
Binary file not shown.
Binary file modified Content/Maps/warmup.umap
Binary file not shown.
66 changes: 8 additions & 58 deletions Source/Cloud9/Character/Cloud9Character.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Copyright Epic Games, Inc. All Rights Reserved.

#include "Cloud9Character.h"

#include "DrawDebugHelpers.h"
Expand Down Expand Up @@ -31,9 +29,6 @@ ACloud9Character::ACloud9Character(const FObjectInitializer& ObjectInitializer)
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;

RotationSpeed = 8.0f;
TargetRotation = GetActorRotation();

GetCapsuleComponent()->InitCapsuleSize(32.f, 72.0f);

const auto Movement = GetCharacterMovement();
Expand Down Expand Up @@ -100,7 +95,7 @@ void ACloud9Character::UnSneak() const
}
}

void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHitValid)
void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHitValid) const
{
if (IsValid(CursorToWorld))
{
Expand Down Expand Up @@ -150,7 +145,7 @@ void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHit
const auto TargetLocation = FVector{HitResult.Location.X, HitResult.Location.Y, 0.0f};
const auto ActorLocation = GetActorLocation();
const auto LookRotation = UKismetMathLibrary::FindLookAtRotation(ActorLocation, TargetLocation);
TargetRotation.Yaw = LookRotation.Yaw;
GetCloud9CharacterMovement()->Rotate({0.0f, LookRotation.Yaw, 0.0f});
}
}

Expand Down Expand Up @@ -207,35 +202,23 @@ void ACloud9Character::OnConstruction(const FTransform& Transform)

const auto Rotator = Transform.Rotator();

TargetRotation.Yaw = Rotator.Yaw;
SetCameraRotationYaw(Rotator.Yaw);
SetActorRotation(TargetRotation);
GetCloud9CharacterMovement()->Rotate({0.0f, Rotator.Yaw, 0.0f}, true);

SetCursorIsHidden(true);

if (IsValid(CursorDecal))
if (CursorDecal != nullptr)
{
UE_LOG(LogCloud9, Display, TEXT("Setup CursorDecal = %s"), *CursorDecal->GetName());
CursorToWorld->SetDecalMaterial(CursorDecal);
CursorToWorld->DecalSize = FVector(16.0f, 32.0f, 32.0f);
}

if (IsValid(GetMesh()))
if (GetMesh() != nullptr && !CameraTargetBoneName.IsNone())
{
if (!CameraTargetBoneName.IsNone())
{
const auto HeadBoneLocation = GetMesh()->GetBoneLocation(CameraTargetBoneName, EBoneSpaces::WorldSpace);
CameraBoom->SetWorldLocation(HeadBoneLocation);
}

const auto Box = UCloud9ToolsLibrary::GetAccurateReferencePoseBounds(GetMesh()->SkeletalMesh);

UE_LOG(LogCloud9, Display, TEXT("Box = %s GetMesh()->Bounds = %s"), *Box.ToString(),
*GetMesh()->Bounds.ToString());

// float Width = 0.0f, Height = 0.0f, Depth = 0.0f;
// UCloud9ToolsLibrary::GetWidthHeightDepth(GetMesh()->Bounds.GetBox(), Width, Height, Depth);
// GetCapsuleComponent()->InitCapsuleSize(Width, Height);
const auto HeadBoneLocation = GetMesh()->GetBoneLocation(CameraTargetBoneName, EBoneSpaces::WorldSpace);
UE_LOG(LogCloud9, Display, TEXT("Setup CameraBoom = %s"), *HeadBoneLocation.ToString());
CameraBoom->SetWorldLocation(HeadBoneLocation);
}
}

Expand All @@ -247,37 +230,4 @@ void ACloud9Character::BeginPlay()
void ACloud9Character::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);

const auto ActorRotation = GetActorRotation();
const auto NewRotation = FMath::Lerp(ActorRotation, TargetRotation, RotationSpeed * DeltaSeconds);
SetActorRotation(NewRotation);

// const auto ActorRotation = GetActorRotation();
// ActorRotation.GetNormalized()
// const auto Remain = TargetRotation - ActorRotation;
// if (Remain != FRotator::ZeroRotator)
// {
// const auto Delta = RotationSpeed * DeltaSeconds;
//
// auto DeltaRotation = FRotator{
// FMath::Min(Delta, Remain.Pitch),
// FMath::Min(Delta, Remain.Yaw),
// FMath::Min(Delta, Remain.Roll),
// };
//
// if (Remain.Pitch > PI)
// DeltaRotation.Pitch = -DeltaRotation.Pitch;
//
// if (Remain.Yaw > PI)
// DeltaRotation.Yaw = -DeltaRotation.Yaw;
//
// if (Remain.Roll > PI)
// DeltaRotation.Roll = -DeltaRotation.Roll;
//
// UE_LOG(LogCloud9, Display, TEXT("%s | %s"),
// *ActorRotation.ToString(),
// *DeltaRotation.ToString());
//
// AddActorWorldRotation(DeltaRotation);
// }
}
7 changes: 2 additions & 5 deletions Source/Cloud9/Character/Cloud9Character.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ACloud9Character : public ACharacter

void UnSneak() const;

void SetViewDirection(const FHitResult& HitResult, bool bIsHitValid);
void SetViewDirection(const FHitResult& HitResult, bool bIsHitValid) const;

void SetCameraRotationYaw(float Angle) const;
void AddCameraRotationYaw(float Angle) const;
Expand Down Expand Up @@ -83,10 +83,7 @@ class ACloud9Character : public ACharacter
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
UDecalComponent* CursorToWorld;

/** An inventory of the character. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Inventory, meta = (AllowPrivateAccess = "true"))
UCloud9Inventory* Inventory;

float RotationSpeed;

FRotator TargetRotation;
};
59 changes: 46 additions & 13 deletions Source/Cloud9/Character/Components/Cloud9CharacterMovement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,77 @@

UCloud9CharacterMovement::UCloud9CharacterMovement()
{
RotationLag = 30.0f;
TargetRotator = FRotator::ZeroRotator;

MaxWalkSpeed = 500.0f;
MaxSneakSpeed = 270.0f;
MaxWalkSpeedCrouched = 170.0f;

auto& NavAgentProperties = GetNavAgentPropertiesRef();
NavAgentProperties.bCanCrouch = true;
NavAgentProperties.bCanSwim = false;
NavAgentProperties.bCanFly = false;
}

ACloud9Character* UCloud9CharacterMovement::GetMyCharacterOwner() const
ACloud9Character* UCloud9CharacterMovement::GetCloud9CharacterOwner() const
{
if (CharacterOwner == nullptr)
return nullptr;

return Cast<ACloud9Character>(CharacterOwner);
return IsValid(CharacterOwner) ? Cast<ACloud9Character>(CharacterOwner) : nullptr;
}

bool UCloud9CharacterMovement::IsSneaking() const
{
return CharacterOwner && GetMyCharacterOwner()->bIsSneaking;
const auto Owner = GetCloud9CharacterOwner();
return IsValid(Owner) && Owner->bIsSneaking;
}

void UCloud9CharacterMovement::Sneak() const
{
if (CharacterOwner)
GetMyCharacterOwner()->bIsSneaking = true;
if (const auto Owner = GetCloud9CharacterOwner(); IsValid(Owner))
{
Owner->bIsSneaking = true;
}
}

void UCloud9CharacterMovement::UnSneak() const
{
if (CharacterOwner)
GetMyCharacterOwner()->bIsSneaking = false;
if (const auto Owner = GetCloud9CharacterOwner(); IsValid(Owner))
{
Owner->bIsSneaking = false;
}
}

void UCloud9CharacterMovement::Rotate(FRotator Rotator, bool Instant)
{
TargetRotator = Rotator;

if (const auto Owner = GetCloud9CharacterOwner(); Instant && IsValid(Owner))
{
Owner->SetActorRotation(TargetRotator);
}
}

float UCloud9CharacterMovement::GetMaxSpeed() const
{
const auto MaxSpeed = Super::GetMaxSpeed();
if (IsSneaking())
return FMath::Min(MaxSneakSpeed, MaxSpeed);
return MaxSpeed;
return IsSneaking() ? FMath::Min(MaxSneakSpeed, MaxSpeed) : MaxSpeed;
}

void UCloud9CharacterMovement::TickComponent(
float DeltaTime,
ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

if (const auto Owner = GetCloud9CharacterOwner(); IsValid(Owner))
{
auto NewRotation = TargetRotator;
if (RotationLag != 0.0f)
{
const auto ActorRotation = Owner->GetActorRotation();
NewRotation = FMath::Lerp(ActorRotation, TargetRotator, DeltaTime / RotationLag * RotationLagScale);
}
Owner->SetActorRotation(NewRotation);
}
}
23 changes: 19 additions & 4 deletions Source/Cloud9/Character/Components/Cloud9CharacterMovement.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,38 @@ class ACloud9Character;
UCLASS(Blueprintable)
class CLOUD9_API UCloud9CharacterMovement
: public UCharacterMovementComponent
, public ICloud9CharacterComponent
, public ICloud9CharacterComponent
{
GENERATED_BODY()

public:
UCloud9CharacterMovement();

ACloud9Character* GetMyCharacterOwner() const;
ACloud9Character* GetCloud9CharacterOwner() const;

bool IsSneaking() const;

void Sneak() const;

void UnSneak() const;

void Rotate(FRotator Rotator, bool Instant = false);

UPROPERTY(Category="Character Movement: Sneaking", EditAnywhere, BlueprintReadWrite, meta=(ClampMin="0", UIMin="0"))
float MaxSneakSpeed;

virtual float GetMaxSpeed() const override;

virtual void TickComponent(
float DeltaTime,
ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction
) override;

private:
static constexpr float RotationLagScale = 360.0f;

/** Character rotation lag*/
UPROPERTY(Category="Character Movement (Rotation Settings)", EditDefaultsOnly)
float RotationLag;

FRotator TargetRotator;
};
18 changes: 15 additions & 3 deletions Source/Cloud9/Cloud9.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ public Cloud9(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "NavigationSystem", "AIModule", "Niagara" });
}
}
PublicDependencyModuleNames.AddRange(new[]
{
"Core",
"CoreUObject",
"Engine",
"InputCore",
"HeadMountedDisplay",
"NavigationSystem",
"AIModule",
"Niagara"
});

CppStandard = CppStandardVersion.Cpp17;
}
}

0 comments on commit 92896ca

Please sign in to comment.