Hello Guys,
I am a student at computer science and i am working on some software development for school. I would like to try something else with videogames with UE4 but i am really a newbie.
Instead of use BP, i would like tro train my C++ skills, and i tried to make a Character from scracth with a new C++ Class.
I created an Actor Class.
I coded his Component as Camera, Capsule, Skeletal Mesh, Arrow and Spring Arm. I gave him Default’s Skeletal Mesh from ThirstCharacterBP to test if my script worked but when i am Starting the game, my Character is just falling on himself as if there were not Physics asset.
There are two files, header and cpp.
.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/SkeletalMeshComponent.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/ArrowComponent.h"
#include "MyPawn.generated.h"
UCLASS()
class ENIGMAC_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* InputComponent) override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UCapsuleComponent* CapsuleComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
USkeletalMeshComponent* CharMesh;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
USpringArmComponent* SpringArm;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UCameraComponent* Camera;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UArrowComponent* ArrowComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
float MovementForce;
FVector2D MovementInput;
void MoveUp(float Value);
void MoveRight(float Value);
};
.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;
CapsuleComponent = CreateAbstractDefaultSubobject<UCapsuleComponent>("CapsuleComponent");
ArrowComponent = CreateAbstractDefaultSubobject<UArrowComponent>("ArrowComponent");
CharMesh = CreateDefaultSubobject<USkeletalMeshComponent>("CharMesh");
SpringArm = CreateDefaultSubobject<USpringArmComponent>("SpringArm");
Camera = CreateDefaultSubobject<UCameraComponent>("Camera");
RootComponent = CapsuleComponent;
ArrowComponent->SetupAttachment(CapsuleComponent);
CharMesh->SetupAttachment(CapsuleComponent);
SpringArm->SetupAttachment(CapsuleComponent);
Camera->SetupAttachment(SpringArm);
CharMesh->SetSimulatePhysics(true);
MovementForce = 100;
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (!MovementInput.IsZero())
{
//Scale our movement input axis values by 100 units per second
MovementInput = MovementInput.GetSafeNormal() * 100.0f;
FVector NewLocation = GetActorLocation();
NewLocation += GetActorForwardVector() * MovementInput.X * DeltaTime;
NewLocation += GetActorRightVector() * MovementInput.Y * DeltaTime;
SetActorLocation(NewLocation);
}
}
void AMyPawn::SetupPlayerInputComponent(UInputComponent * InputComponent)
{
check(InputComponent);
InputComponent->BindAxis("MoveForward", this, &AMyPawn::MoveUp);
InputComponent->BindAxis("MoveRight", this, &AMyPawn::MoveRight);
}
// Called to bind functionality to input
void AMyPawn::MoveUp(float AxisValue) {
/*FVector ForceToAdd = FVector(1, 0, 0) * MovementForce * Value;
CharMesh->AddForce(ForceToAdd);*/
MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}
void AMyPawn::MoveRight(float AxisValue) {
/*FVector ForceToAdd = FVector(0, 1, 0) * MovementForce * Value;
CharMesh->AddForce(ForceToAdd);*/
MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}
Thanks to help me and i’m sorru for this poor English.