Hi All:
I am playing with a tutorial here: Player Input and Pawns | Unreal Engine Documentation
The first time that I went through the tutorial, the project worked as expected after making a couple small code changes; the cylinder moved around and also grew/shrank with the space bar. The main change was renaming InputComponent to InputComp.
However after playing around and trying a few other things, I realized that the cylinder stopped growing and shrinking. I tried deleting the cylinder (MyPawn) and re-importing/dragging into the project. MyPawn2 however had the same issue; it moved around just fine but did not grow/shrink when pressing the space bar.
I then remade the entire tutorial but in a brand new project. However, the cylinder still does not grow and shrink.
MyPawn.h
#pragma once
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"
UCLASS()
class UECPPPRACTICE2_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values
AMyPawn();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaSeconds) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComp) override;
UPROPERTY(EditAnywhere)
USceneComponent* OurVisibleComponent;
// Input functions
void Move_XAxis(float AxisValue);
void Move_YAxis(float AxisValue);
void StartGrowing();
void StopGrowing();
// Input variables
FVector CurrentVelocity;
bool bGrowing;
};
MyPawn.cpp
#include "UeCppPractice2.h"
#include "MyPawn.h"
#include "Camera/CameraComponent.h"
// Sets default values
AMyPawn::AMyPawn()
{
PrimaryActorTick.bCanEverTick = true;
AutoPossessPlayer = EAutoReceiveInput::Player0;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
OurCamera->SetupAttachment(RootComponent);
OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
OurVisibleComponent->SetupAttachment(RootComponent);
}
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
{
float CurrentScale = OurVisibleComponent->GetComponentScale().X;
if (bGrowing)
{
CurrentScale += DeltaTime;
}
else
{
CurrentScale -= (DeltaTime * 0.5f);
}
CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
}
// Handle movement based on our "MoveX" and "MoveY" axes
{
if (!CurrentVelocity.IsZero())
{
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
SetActorLocation(NewLocation);
}
}
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComp)
{
Super::SetupPlayerInputComponent(InputComp);
InputComp->BindAction("Grow", IE_Pressed, this, &AMyPawn::StartGrowing);
InputComp->BindAction("Grow", IE_Released, this, &AMyPawn::StopGrowing);
InputComp->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
InputComp->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);
}
void AMyPawn::Move_XAxis(float AxisValue)
{
CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}
void AMyPawn::Move_YAxis(float AxisValue)
{
CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}
void AMyPawn::StartGrowing()
{
bGrowing = true;
}
void AMyPawn::StopGrowing()
{
bGrowing = false;
}
Also, after adding some logging it showed that the CurrentScale
value seems to be correct. It’s clamped between 1.0 and 2.0 just before attempting SetWorldScale3d:
CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
UE_LOG(LogTemp, Warning, TEXT("CurrentScale: %f"), CurrentScale);
OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
The CurrentScale
variable increases slowly while pressing spacebar and then gradually decreases when letting go of the spacebar.
Any ideas as to what is wrong? Thank you!