22.1 C
New York
Monday, September 22, 2025

c++ – Utilizing UPawnMovementComponent to regulate a Pawn with Keyboard sharing code between Pawns


I’ve simply began to study Unreal and studying the tutorial Elements and Collisions I’ve discovered that they use the UPawnMovementComponent to regulate their Pawn.

Earlier than studying this tutorial I’ve added the code to maneuver the Pawn inside its C++ code:

#embrace "Paddle.h"
#embrace "Elements/BoxComponent.h"
#embrace "Elements/StaticMeshComponent.h"

// Units default values
APaddle::APaddle()
{
    // Set this pawn to name Tick() each body.  You'll be able to flip this off to enhance efficiency if you happen to do not want it.
    PrimaryActorTick.bCanEverTick = true;

    // Set this pawn to be managed by the lowest-numbered participant
    AutoPossessPlayer = EAutoReceiveInput::Player0;

    VisualComponent = CreateDefaultSubobject(TEXT("VisualComp"));
    RootComponent = VisualComponent;

    // Eliminated for brevety

    CurrentVelocity.Z = 0.0f;

}

// Known as when the sport begins or when spawned
void APaddle::BeginPlay()
{
    Tremendous::BeginPlay();
    
}

// Known as each body
void APaddle::Tick(float DeltaTime)
{
    Tremendous::Tick(DeltaTime);

    // Deal with motion based mostly on our "MoveZ" axis.
    if (!CurrentVelocity.IsZero())
    {
        const FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
        SetActorLocation(NewLocation);
    }

}

// Known as to bind performance to enter
void APaddle::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Tremendous::SetupPlayerInputComponent(PlayerInputComponent);

    // Reply each body to the values of our motion.
    InputComponent->BindAxis(TEXT("MovePaddle"), this, &APaddle::Move_ZAxis);
}

void APaddle::Move_ZAxis(float AxisValue)
{
    CurrentVelocity.Z = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

A Tip from that tutorial says:

Pawn Motion Elements have some highly effective, built-in options to assist with frequent physics performance, and are a great way to share motion code between many Pawn sorts. Utilizing Elements to separate performance is an efficient apply to scale back muddle as your challenge grows and your Pawns turn out to be extra complicated.

They are saying that utilizing Pawn Motion Elements is an efficient method to share motion code between many Pawn however they’ve the identical code than me inside their Pawn element:

// Copyright 1998-2018 Epic Video games, Inc. All Rights Reserved.

#embrace "CollidingPawn.h"
#embrace "CollidingPawnMovementComponent.h"
#embrace "UObject/ConstructorHelpers.h"
#embrace "Particles/ParticleSystemComponent.h"
#embrace "Elements/SphereComponent.h"
#embrace "Digicam/CameraComponent.h"
#embrace "GameFramework/SpringArmComponent.h"

// Units default values
ACollidingPawn::ACollidingPawn()
{
    // Set this pawn to name Tick() each body.  You'll be able to flip this off to enhance efficiency if you happen to do not want it.
    PrimaryActorTick.bCanEverTick = true;

    // Eliminated for brevety

    // Take management of the default participant
    AutoPossessPlayer = EAutoReceiveInput::Player0;

    // Create an occasion of our motion element, and inform it to replace our root element.
    OurMovementComponent = CreateDefaultSubobject(TEXT("CustomMovementComponent"));
    OurMovementComponent->UpdatedComponent = RootComponent;
}

// Known as when the sport begins or when spawned
void ACollidingPawn::BeginPlay()
{
    Tremendous::BeginPlay();

}

// Known as each body
void ACollidingPawn::Tick( float DeltaTime )
{
    Tremendous::Tick( DeltaTime );

}

// Known as to bind performance to enter
void ACollidingPawn::SetupPlayerInputComponent(class UInputComponent* InInputComponent)
{
    Tremendous::SetupPlayerInputComponent(InInputComponent);

    InInputComponent->BindAction("ParticleToggle", IE_Pressed, this, &ACollidingPawn::ParticleToggle);

    InInputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward);
    InInputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight);
    InInputComponent->BindAxis("Flip", this, &ACollidingPawn::Flip);
}

UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const
{
    return OurMovementComponent;
}

void ACollidingPawn::MoveForward(float AxisValue)
{
    if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
    {
        OurMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue);
    }
}

void ACollidingPawn::MoveRight(float AxisValue)
{
    if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
    {
        OurMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);
    }
}

void ACollidingPawn::Flip(float AxisValue)
{
    FRotator NewRotation = GetActorRotation();
    NewRotation.Yaw += AxisValue;
    SetActorRotation(NewRotation);
}

void ACollidingPawn::ParticleToggle()
{
    if (OurParticleSystem && OurParticleSystem->Template)
    {
        OurParticleSystem->ToggleActive();
    }
}

They’ve the identical SetupPlayerInputComponent technique and likewise three strategies to maneuver the Pawn contained in the Pawn class. I do not know the way they’ll share transfer code between Pawn if the code is inside every Pawn and the way we are able to separate performance if all of the motion code is contained in the Pawns.

How can I exploit UPawnMovementComponent to regulate a Pawn with Keyboard sharing that code with different Pawns?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles