Added Crunch and Finished Animation with Foot IK

This commit is contained in:
Caleb Buhungiro
2025-06-17 19:29:52 +08:00
parent b638fe466c
commit 9a6a072b5a
1601 changed files with 5127 additions and 1 deletions

View File

@@ -0,0 +1,50 @@
// Multiplayer By Caleb
#include "Animations/CAnimInstance.h"
#include "GameFramework/Character.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h"
void UCAnimInstance::NativeInitializeAnimation()
{
Super::NativeInitializeAnimation();
OwnerCharacter = Cast<ACharacter>(TryGetPawnOwner());
if (OwnerCharacter)
{
OwnerMovementComp = OwnerCharacter->GetCharacterMovement();
}
}
void UCAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
Super::NativeUpdateAnimation(DeltaSeconds);
if (OwnerCharacter)
{
Speed = OwnerCharacter->GetVelocity().Length();
const auto BodyRot{OwnerCharacter->GetActorRotation()};
const auto BodyRotDelta{UKismetMathLibrary::NormalizedDeltaRotator(BodyRot, BodyPrevRot)};
BodyPrevRot = BodyRot;
YawSpeed = BodyRotDelta.Yaw / DeltaSeconds;
SmoothedYawSpeed = UKismetMathLibrary::FInterpTo(
SmoothedYawSpeed,
YawSpeed,
DeltaSeconds,
YawSpeedSmoothLerpSpeed
);
const auto ControlRot{OwnerCharacter->GetBaseAimRotation()};
LookRotOffset = UKismetMathLibrary::NormalizedDeltaRotator(ControlRot, BodyRot);
}
if (OwnerMovementComp)
{
bIsJumping = OwnerMovementComp->IsFalling();
}
}
void UCAnimInstance::NativeThreadSafeUpdateAnimation(float DeltaSeconds)
{
Super::NativeThreadSafeUpdateAnimation(DeltaSeconds);
}

View File

@@ -0,0 +1,29 @@
// Multiplayer By Caleb
#include "Crunch/Public/Character/CCharacter.h"
// Sets default values
ACCharacter::ACCharacter()
{
PrimaryActorTick.bCanEverTick = true;
GetMesh()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
void ACCharacter::BeginPlay()
{
Super::BeginPlay();
}
void ACCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ACCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}

View File

@@ -0,0 +1,97 @@
// Multiplayer By Caleb
#include "Crunch/Public/Character/CPlayerCharacter.h"
#include "Camera/CameraComponent.h"
#include "Crunch/Public/Player/CPlayerController.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
ACPlayerCharacter::ACPlayerCharacter()
{
PrimaryActorTick.bCanEverTick = true;
CameraBoom = CreateDefaultSubobject<USpringArmComponent>("CameraBoom");
CameraBoom->SetupAttachment(GetRootComponent());
CameraBoom->bUsePawnControlRotation = true;
ViewCamera = CreateDefaultSubobject<UCameraComponent>("ViewCamera");
ViewCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->RotationRate = RotationRate;
}
void ACPlayerCharacter::PawnClientRestart()
{
Super::PawnClientRestart();
if (const auto OwningPlayerController{GetController<ACPlayerController>()})
{
const auto InputSubsystem{
OwningPlayerController->GetLocalPlayer()->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>()
};
if (InputSubsystem)
{
InputSubsystem->RemoveMappingContext(GameplayMappingContext);
InputSubsystem->AddMappingContext(GameplayMappingContext, 0);
}
}
}
void ACPlayerCharacter::BeginPlay()
{
Super::BeginPlay();
}
void ACPlayerCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ACPlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (const auto EnhancedInputComp{Cast<UEnhancedInputComponent>(PlayerInputComponent)})
{
EnhancedInputComp->BindAction(JumpInputAction, ETriggerEvent::Triggered, this, &ThisClass::Jump);
EnhancedInputComp->BindAction(LookInputAction, ETriggerEvent::Triggered, this, &ThisClass::HandleLookInput);
EnhancedInputComp->BindAction(MoveInputAction, ETriggerEvent::Triggered, this, &ThisClass::HandleMoveInput);
}
}
void ACPlayerCharacter::HandleLookInput(const FInputActionValue& InputActionValue)
{
const FVector2D InputVal{InputActionValue.Get<FVector2D>()};
AddControllerPitchInput(-InputVal.Y);
AddControllerYawInput(InputVal.X);
}
void ACPlayerCharacter::HandleMoveInput(const FInputActionValue& InputActionValue)
{
FVector2D InputVal{InputActionValue.Get<FVector2D>()};
InputVal.Normalize();
AddMovementInput(GetMoveFwdDir() * InputVal.Y + GetLookRightDir() * InputVal.X);
}
FVector ACPlayerCharacter::GetLookRightDir() const
{
return ViewCamera->GetRightVector();
}
FVector ACPlayerCharacter::GetLookFwdDir() const
{
return ViewCamera->GetForwardVector();
}
FVector ACPlayerCharacter::GetMoveFwdDir() const
{
return FVector::CrossProduct(GetLookRightDir(), FVector::UpVector);
}

View File

@@ -0,0 +1,4 @@
// Multipalyer By Caleb
#include "Crunch/Public/Game/CGameMode.h"

View File

@@ -0,0 +1,5 @@
// Multipalyer By Caleb
#include "Crunch/Public/Player/CPlayerController.h"

View File

@@ -0,0 +1,64 @@
// Multiplayer By Caleb
#pragma once
#include "CoreMinimal.h"
#include "Animation/AnimInstance.h"
#include "CAnimInstance.generated.h"
/**
*
*/
UCLASS()
class CRUNCH_API UCAnimInstance : public UAnimInstance
{
GENERATED_BODY()
public:
virtual void NativeInitializeAnimation() override;
virtual void NativeUpdateAnimation(float DeltaSeconds) override;
virtual void NativeThreadSafeUpdateAnimation(float DeltaSeconds) override;
UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
FORCEINLINE float GetSpeed() const { return Speed; }
UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
FORCEINLINE bool IsMoving() const { return Speed != 0; }
UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
FORCEINLINE bool IsNotMoving() const { return Speed == 0; }
UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
FORCEINLINE float GetYawSpeed() const { return YawSpeed; }
UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
FORCEINLINE float GetSmoothedYaw() const { return SmoothedYawSpeed; }
UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
FORCEINLINE bool GetIsJumping() const { return bIsJumping; }
UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
FORCEINLINE bool IsOnGround() const { return !bIsJumping; }
UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
FORCEINLINE float GetLookYawOffset() const { return LookRotOffset.Yaw; }
UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
FORCEINLINE float GetLookPitchOffset() const { return LookRotOffset.Pitch; }
private:
UPROPERTY()
class ACharacter* OwnerCharacter;
UPROPERTY()
class UCharacterMovementComponent* OwnerMovementComp;
UPROPERTY()
float Speed{0.f};
float YawSpeed{0.f};
float SmoothedYawSpeed{0.f};
bool bIsJumping{false};
UPROPERTY(EditAnywhere, Category="Animation")
float YawSpeedSmoothLerpSpeed{1.f};
FRotator BodyPrevRot{};
FRotator LookRotOffset{};
};

View File

@@ -0,0 +1,24 @@
// Multiplayer By Caleb
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "CCharacter.generated.h"
UCLASS()
class CRUNCH_API ACCharacter : public ACharacter
{
GENERATED_BODY()
public:
ACCharacter();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};

View File

@@ -0,0 +1,52 @@
// Multiplayer By Caleb
#pragma once
#include "CoreMinimal.h"
#include "CCharacter.h"
#include "CPlayerCharacter.generated.h"
struct FInputActionValue;
UCLASS()
class CRUNCH_API ACPlayerCharacter : public ACCharacter
{
GENERATED_BODY()
public:
ACPlayerCharacter();
virtual void PawnClientRestart() override;
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
private:
UPROPERTY(VisibleDefaultsOnly, Category= "View")
class USpringArmComponent* CameraBoom;
UPROPERTY(VisibleDefaultsOnly, Category= "View")
class UCameraComponent* ViewCamera;
UPROPERTY(EditDefaultsOnly, Category= "View")
FRotator RotationRate{FRotator(0.0f, 720.0f, 0.0f)};
UPROPERTY(EditDefaultsOnly, Category= "Input")
class UInputMappingContext* GameplayMappingContext;
UPROPERTY(EditDefaultsOnly, Category= "Input")
class UInputAction* JumpInputAction;
UPROPERTY(EditDefaultsOnly, Category= "Input")
UInputAction* LookInputAction;
UPROPERTY(EditDefaultsOnly, Category= "Input")
UInputAction* MoveInputAction;
void HandleLookInput(const FInputActionValue& InputActionValue);
void HandleMoveInput(const FInputActionValue& InputActionValue);
FVector GetLookRightDir() const;
FVector GetLookFwdDir() const;
FVector GetMoveFwdDir() const;
};

View File

@@ -0,0 +1,16 @@
// Multipalyer By Caleb
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "CGameMode.generated.h"
/**
*
*/
UCLASS()
class CRUNCH_API ACGameMode : public AGameModeBase
{
GENERATED_BODY()
};

View File

@@ -0,0 +1,16 @@
// Multipalyer By Caleb
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "CPlayerController.generated.h"
/**
*
*/
UCLASS()
class CRUNCH_API ACPlayerController : public APlayerController
{
GENERATED_BODY()
};