Hi Guys, I’ve written a c++ code for a First Person camera, based on the UE4 templates and the First Person Shooter tutorial found on the wiki page. Here’s what I’ve got:
.h
GENERATED_UCLASS_BODY()
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
TSubobjectPtr<class UCameraComponent>MainCamera;
UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
TSubobjectPtr<USkeletalMeshComponent>FPChar;
bool bIsHeroCrouched;
protected:
void MoveForward(float value);
void MoveRight(float value);
void CrouchHero();
void Tick(float DeltaTime);
void SetupPlayerInputComponent(UInputComponent *InputComponent);
.cpp:
AUECharHero::AUECharHero(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
//Inicializa el tamaño del CapsuleComponent para el personaje, creo que tiene que ver con las físicas..?
CapsuleComponent->InitCapsuleSize(46.0f, 96.0f);
bIsHeroCrouched = false;
//Usar el pitch, yaw y rooll del control en el control del personaje.
bUseControllerRotationPitch = true;
bUseControllerRotationRoll = true;
bUseControllerRotationYaw = true;
//Cración de la cámara
MainCamera = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("CharCamera"));
MainCamera->AttachParent = CapsuleComponent;//Con eso se atacha al Capsule conponent cono un Child.
MainCamera->RelativeLocation = FVector(0.0f, 0.0f, 10.0f + BaseEyeHeight);//Se fija la posición de la camara
//Se crea para el personaje en primera persona
FPChar = PCIP.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("FPChar"));
FPChar->SetOnlyOwnerSee(true); //solo el que juega va a verlo, los demás verían el personaje completo
FPChar->AttachParent = MainCamera; //se atacha a la cámara
FPChar->bCastDynamicShadow = false; //No crea sombras
FPChar->CastShadow = false;
Mesh->SetOwnerNoSee(true);
//CharacterMovement->bOrientRotationToMovement = false;
}
void AUECharHero::SetupPlayerInputComponent(UInputComponent *InputComponent)
{
check(InputComponent);
InputComponent->BindAxis("MoveForward", this, &AUECharHero::MoveForward);
InputComponent->BindAxis("MoveRight", this, &AUECharHero::MoveRight);
InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
InputComponent->BindAxis("Turn", this, &ACharacter::AddControllerYawInput);
InputComponent->BindAxis("LookUp", this, &ACharacter::AddControllerPitchInput);
InputComponent->BindAction("Crouch", IE_Pressed, this, &AUECharHero::CrouchHero);
}
void AUECharHero::MoveRight(float value)
{
if (Controller!=NULL && value != 0.0f)
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get right vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement in that direction
if (!bIsHeroCrouched)
{
AddMovementInput(Direction, value);
}
else
{
AddMovementInput(Direction, value/2.0f);
}
}
}
void AUECharHero::Tick(float DeltaTime)
{
FRotator Rotacion = Controller->GetControlRotation();
FString Message;
Message = Rotacion.ToString();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(1, 1.0f, FColor::Blue, Message);
}
if (bIsHeroCrouched)
{
GEngine->AddOnScreenDebugMessage(2, 1.0f, FColor::Cyan, TEXT("Is Crouched"));
}
}
void AUECharHero::MoveForward(float value)
{
if (Controller !=NULL && value != 0.0f)
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
if (!bIsHeroCrouched)
{
AddMovementInput(Direction, value);
}
else
{
AddMovementInput(Direction, value/2.0f);
}
}
}
void AUECharHero::CrouchHero()
{
if (Controller != NULL)
{
if (!bIsHeroCrouched)
{
bIsHeroCrouched = true;
}
else
{
bIsHeroCrouched = false;
}
}
}
As I`m relatively new writing code in C++, i wanna know ¿is it efficient?, ¿Is there a better way to implement it?, Any comment on the code will be really appreciated.
Thanks!!