APawn class, add axis mapping to enable control of APawn movement
MyPawn.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/StaticMeshComponent.h"
#include "Camera/CameraComponent.h"
#include "MyPawn.generated.h"
UCLASS()
class FIRSTDEMO_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* MyStaticMesh;
UPROPERTY(VisibleAnywhere)
UCameraComponent* MyCamera;
UPROPERTY(EditAnywhere)
float MaxSpeed;//add pawn movement speed parameter.
private:
// add axis mapping function, MoveForward, MoveRight, Velocity
//Set to private variables, handled in the Pawn class itself, no need to expose them to outsiders.
void MoveForward(float Value);
void MoveRight(float Value);
FVector Velocity;
};
MyPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Components/InputComponent.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;
RootComponent=CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
MyStaticMesh->SetupAttachment(GetRootComponent());
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(GetRootComponent());
MyCamera->SetRelativeLocation(FVector(-300.f, 0.f, 300.f));
MyCamera->SetRelativeRotation(FRotator(-45.f, 0.f, 0.f));
AutoPossessPlayer = EAutoReceiveInput::Player0;
MaxSpeed = 100.f;
Velocity = FVector::ZeroVector;
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//Add offset function to tick.
AddActorLocalOffset(Velocity * DeltaTime, true);
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Bind player input to axis mapping, axis mapping and function name should correspond to each other
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AMyPawn::MoveForward);
PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AMyPawn::MoveRight);
}
void AMyPawn::MoveForward(float Value)
{
//Forward speed, use Clamp to limit value to -1,1, place too large a value in the Ue4 editor, resulting in a bug.
Velocity.X = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}
void AMyPawn::MoveRight(float Value)
{
// Speed to the right, use Clamp to limit value to -1,1, place too large a value in the Ue4 editor, resulting in a bug.
Velocity.Y = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}
Read More:
- Matplotlib of data visualization plt.xlim The () ylim() function sets the x-axis and y-axis range coordinates
- Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters:
- Altium designer failed to add class member problem
- Altium designer20 failed to add class member: XXX and unknown pin
- Add a new springboot, and it will appear unable to read meta data for class when it is dependent
- How to Fix IWAB0489E Error when deploying Web service to Axis runtime
- Add Samba user prompt failed to add entry for user
- Browser prompt: Source mapping error: request failed with status 404 Source URL: http://xxx.js Source mapping URL: jquery.min.map
- On the usage of ‘ref.stor.type search’ field in SAP WM movement type
- Add samba user prompt Failed to add entry for user.
- MySQL failed to add foreign key: SQL 1452 cannot add or update a child row:a foreign key constraint fails
- valueError: Length mismatch: Expected axis has 40 elements, new values have 38 elements
- This application has no explicit mapping for /error, so you are seeing this as a fallback (How to Fix)
- Failed to get HbA FCP target mapping in lanfree backup
- Error 3002: Problem in mapping fragments | c# linq to entities
- Error c2011: “a certain class”: redefinition of “class” type
- VTK notes — texture mapping
- Errors encountered by elasticsearch in creating index and mapping
- org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file
- Using code to create control objects, set the event listening method does not execute