I am using blank unreal engine 5.4 c++ project. I am trying to create a pawn and give it manny as mesh and animation instance.
MyPawn.h
#pragma once
include “CoreMinimal.h”
include “GameFramework/Pawn.h”
include “MyPawn.generated.h”
class USpringArmComponent;
class UCameraComponent;
class UInputMappingContext;
class UInputAction;
struct FInputActionValue;
class UCapsuleComponent;
class UArrowComponent;
class USceneComponent;
class USkeletalMeshComponent;
class UPawnMovementComponent;
UCLASS()
class SBPAWNGRAVITY_API AMyPawn : public APawn
{
GENERATED_BODY()
/** Camera boom positioning the camera behind the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
USpringArmComponent* CameraBoom;
/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
UCameraComponent* FollowCamera;
/** MappingContext */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputMappingContext* DefaultMappingContext;
/** Jump Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* JumpAction;
/** Move Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* MoveAction;
/** Look Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* LookAction;
/** Set Movement Component */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UPawnMovementComponent* MovementComponent;
/** The CapsuleComponent being used for movement collision (by CharacterMovement).*/
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Capusle, meta = (AllowPrivateAccess = "true"))
UCapsuleComponent* CapsuleComponent;
/** Skeletal mesh associated with this Pawn. */
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Mesh, meta = (AllowPrivateAccess = "true"))
USkeletalMeshComponent* PawnMesh;
/** Gizmo used as debug arrows root component. */
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Root, meta = (AllowPrivateAccess = "true"))
USceneComponent* GizmoRootComponent;
/**Forward Arrow Component*/
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Arror, meta = (AllowPrivateAccess = "true"))
UArrowComponent* ForwardArrowComponent;
/**Right Arrow Component*/
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Arror, meta = (AllowPrivateAccess = "true"))
UArrowComponent* RightArrowComponent;
/**Up Arrow Component. */
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Arror, meta = (AllowPrivateAccess = "true"))
UArrowComponent* UpArrowComponent;
public:
// Sets default values for this pawn’s properties
AMyPawn();
protected:
/** Called for movement input */
void Move(const FInputActionValue& Value);
/** Called for looking input */
void Look(const FInputActionValue& Value);
public:
// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// To add mapping context
virtual void BeginPlay();
public:
/** Returns CapsuleComponent subobject /
FORCEINLINE class UCapsuleComponent GetCapsuleComponent() const { return CapsuleComponent; }
/* Returns PawnMesh subobject /
FORCEINLINE class USkeletalMeshComponent GetMesh() const { return PawnMesh; }
/* Returns CameraBoom subobject /
FORCEINLINE class USpringArmComponent GetCameraBoom() const { return CameraBoom; }
/* Returns FollowCamera subobject /
FORCEINLINE class UCameraComponent GetFollowCamera() const { return FollowCamera; }
/* Returns movement subobject /
FORCEINLINE class UPawnMovementComponent GetMovementComponent() const { return MovementComponent; }
/* Returns Gizmo SceneComponent subobject /
FORCEINLINE class USceneComponent GetGizmoRootComponent() const { return GizmoRootComponent; }
/* Returns Forward ArrowComponent subobject /
FORCEINLINE class UArrowComponent GetForwardArrowComponent() const { return ForwardArrowComponent; }
/* Returns Right ArrowComponent subobject /
FORCEINLINE class UArrowComponent GetRightArrowComponent() const { return RightArrowComponent; }
/* Returns Up ArrowComponent subobject */
FORCEINLINE class UArrowComponent GetUpArrowComponent() const { return UpArrowComponent; }
};
MyPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
include “MyPawn.h”
include “Engine/LocalPlayer.h”
include “Camera/CameraComponent.h”
include “Components/CapsuleComponent.h”
include “GameFramework/FloatingPawnMovement.h”
include “Components/ArrowComponent.h”
include “GameFramework/SpringArmComponent.h”
include “GameFramework/Controller.h”
include “EnhancedInputComponent.h”
include “EnhancedInputSubsystems.h”
include “InputActionValue.h”
include “Components/SkeletalMeshComponent.h”
include “Components/SkinnedMeshComponent.h”
// Sets default values
AMyPawn::AMyPawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;
// Set size for collision capsule
CapsuleComponent = CreateDefaultSubobject<UCapsuleComponent>(TEXT("CollisionCapsule0"));
if (CapsuleComponent)
{
CapsuleComponent->InitCapsuleSize(42.0f, 96.0f);
CapsuleComponent->SetCollisionProfileName(TEXT("Pawn"));
CapsuleComponent->SetSimulatePhysics(true);
CapsuleComponent->SetEnableGravity(false);
RootComponent = CapsuleComponent;
CapsuleComponent->GetBodyInstance()->COMNudge = FVector(0.0f, 0.0f, -96.0f);
CapsuleComponent->SetLinearDamping(0.15f);
CapsuleComponent->SetAngularDamping(100.0f);
CapsuleComponent->SetNotifyRigidBodyCollision(true);
}
// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Create a camera boom (pulls in towards the player if there is a collision)
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
if (CameraBoom)
{
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 400.0f; // The camera follows at this distance behind the character
CameraBoom->ProbeSize = 15.0f;
CameraBoom->ProbeChannel = ECollisionChannel::ECC_Camera;
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
//CameraBoom->bInheritPitch = true;
//CameraBoom->bInheritYaw = true;
//CameraBoom->bInheritRoll = true;
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
CameraBoom->SetupAttachment(CapsuleComponent);
}
// Create a follow camera
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
if (FollowCamera)
{
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
}
// pawn mesh
PawnMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("PawnMesh0"));
if (PawnMesh)
{
PawnMesh->VisibilityBasedAnimTickOption = EVisibilityBasedAnimTickOption::AlwaysTickPose;
PawnMesh->bCastDynamicShadow = true;
PawnMesh->bAffectDynamicIndirectLighting = true;
PawnMesh->PrimaryComponentTick.TickGroup = TG_PrePhysics;
PawnMesh->SetCollisionProfileName(TEXT("CharacterMesh"));
PawnMesh->SetGenerateOverlapEvents(false);
PawnMesh->SetNotifyRigidBodyCollision(false);
PawnMesh->SetupAttachment(CapsuleComponent);
}
MovementComponent = CreateDefaultSubobject<UFloatingPawnMovement>(TEXT("MovementComponent"));
if (MovementComponent)
{
MovementComponent->SetUpdatedComponent(CapsuleComponent);
}
GizmoRootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("GizmoRootComponent0"));
if (GizmoRootComponent)
{
GizmoRootComponent->SetupAttachment(CapsuleComponent);
ForwardArrowComponent = CreateDefaultSubobject<UArrowComponent>(TEXT("ForwardArrowComponent0"));
if (ForwardArrowComponent)
{
ForwardArrowComponent->ArrowColor = FColor::Red;
ForwardArrowComponent->bTreatAsASprite = true;
ForwardArrowComponent->SetupAttachment(GizmoRootComponent);
ForwardArrowComponent->bIsScreenSizeScaled = true;
ForwardArrowComponent->SetRelativeRotation(FRotator(0.0f, 0.0f, 0.0f));
}
RightArrowComponent = CreateDefaultSubobject<UArrowComponent>(TEXT("RightArrowComponent0"));
if (RightArrowComponent)
{
RightArrowComponent->ArrowColor = FColor::Green;
RightArrowComponent->bTreatAsASprite = true;
RightArrowComponent->SetupAttachment(GizmoRootComponent);
RightArrowComponent->bIsScreenSizeScaled = true;
RightArrowComponent->SetRelativeRotation(FRotator(0.0f, 90.0f, 0.0f));
}
UpArrowComponent = CreateDefaultSubobject<UArrowComponent>(TEXT("UpArrowtComponent0"));
if (UpArrowComponent)
{
UpArrowComponent->ArrowColor = FColor::Blue;
UpArrowComponent->bTreatAsASprite = true;
UpArrowComponent->SetupAttachment(GizmoRootComponent);
UpArrowComponent->bIsScreenSizeScaled = true;
UpArrowComponent->SetRelativeRotation(FRotator(90.0f, 0.0f, 0.0f));
}
GizmoRootComponent->SetVisibility(true, true);
GizmoRootComponent->SetHiddenInGame(true, true);
}
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
// Call the base class
Super::BeginPlay();
}
//////////////////////////////////////////////////////////////////////////
// Input
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
// Add Input Mapping Context
if (APlayerController* PlayerController = Cast(GetController()))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent)) {
// Moving
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyPawn::Move);
// Looking
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMyPawn::Look);
}
}
void AMyPawn::Move(const FInputActionValue& Value)
{
// input is a Vector2D
FVector2D MovementVector = Value.Get();
if (Controller != nullptr)
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
// get right vector
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void AMyPawn::Look(const FInputActionValue& Value)
{
// input is a Vector2D
FVector2D LookAxisVector = Value.Get();
if (Controller != nullptr)
{
// add yaw and pitch input to controller
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
Can anyone tell me what is the issue? I am new to Unreal Engine. And I am curious to check whether Manny Mesh works with Pawn or not.