Unreal-Engine-C-Programming-Pawan And Input-Getting-With-part-5/10
First We Make Pawn Class
Pawn means Actor this controlled by Input with Humans or AI
Second Select Pawn
Create!
Open Header file
UPROPERTY(EditAnywhere)
USceneComponent* OurVisibleComponent;
Goes to cpp file
Include files
#include "Components/StaticMeshComponent.h"
#include "Components/InputComponent.h"
AutoPossessPlayer = EAutoReceiveInput::Player0;
Setup this code
AMyPawnWithInput::AMyPawnWithInput()
{
// 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;
AutoPossessPlayer = EAutoReceiveInput::Player0;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
// Create a camera and a visible object
UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>
(TEXT("OurCamera"));
OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>
(TEXT("OurVisibleComponent"));
// Attach our camera and visible object to our root component.
//Offset and rotate the camera.
OurCamera->SetupAttachment(RootComponent);
OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
OurVisibleComponent->SetupAttachment(RootComponent);
}
Compile!!
Goes to Edit -> Project Setting find Input
Click on Input Find Axis Bind and Action Bind
Axis Bind – MoveForward (W(1.0),S(-1.0)) and MoveRight(A(-1.0),D(1.0))
Action Bind – Jump (SpaceBar)
Header file
bool gGrooing;
void MoveForward(float Axis);
void MoveRight(float Axis);
void JumpStart();
void JumpStop();
//Input Variables
FVector CurrentVelocity;
Cpp file
First
void AMyPawnWithInput::MoveForward(float Axis) {
CurrentVelocity.X = FMath::Clamp(Axis, -1.0f, 1.0f) * 100.0f ;
}
void AMyPawnWithInput::MoveRight(float Axis) {
CurrentVelocity.Y = FMath::Clamp(Axis, -1.0f, 1.0f) * 100.0f;
}
void AMyPawnWithInput::JumpStart() {
gGrooing = true;
}
void AMyPawnWithInput::JumpStop() {
gGrooing = false;
}
Second
void AMyPawnWithInput::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AMyPawnWithInput::JumpStart);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &AMyPawnWithInput::JumpStop);
PlayerInputComponent->BindAxis("MoveForward", this, &AMyPawnWithInput::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AMyPawnWithInput::MoveRight);
}
Ok Goes Tick();
// Called every frame
void AMyPawnWithInput::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
float CurrentScale = OurVisibleComponent->GetComponentScale().X;
if (gGrooing) {
CurrentScale += DeltaTime;
}
else {
CurrentScale -= DeltaTime;
}
if (!CurrentVelocity.IsZero()) {
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime );
SetActorLocation(NewLocation);
}
}
Compile And Play!!
Comments
Post a Comment