Hello,
I was following a tutorial to start UE4 with C++ (and blueprints)
But when I wanted to test if my code was correct, the pawn (UlitmatePawn) wasn’t shooting the Bullet (Actor)
Can somebody please tall me what’s wrong with my code and me ?
Thank’s a lot helping me.
Shawrex
Bullet.cpp :
#include "Bullet.h"
#include "Classes/GameFrameWork/ProjectileMovementComponent.h"
#include "Classes/Components/StaticMeshComponent.h"
// Sets default values
ABullet::ABullet()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
BulletMesh = CreateDefaultSubobject<UStaticMeshComponent>("BulletMesh");
SetRootComponent(BulletMesh);
BulletMovement = CreateDefaultSubobject<UProjectileMovementComponent>("BulletMovement");
BulletMovement->InitialSpeed = 2000.f;
BulletMovement->MaxSpeed = 2000.f;
}
// Called when the game starts or when spawned
void ABullet::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ABullet::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
Bullet.h :
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Bullet.generated.h"
UCLASS()
class ULTIMATE_API ABullet : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ABullet();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere, Category = "Components")
UStaticMeshComponent* BulletMesh;
UPROPERTY(EditAnywhere, Category = "Components")
class UProjectileMovementComponent* BulletMovement;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
UltimatePawn.cpp :
#include "UltimatePawn.h"
#include "Bullet.h"
#include "Classes/Components/InputComponent.h"
#include "Classes/GameFramework/FloatingPawnMovement.h"
#include "Classes/GameFramework/SpringArmComponent.h"
#include "Classes/Camera/CameraComponent.h"
#include "Runtime/Engine/Classes/Engine/World.h"
// Sets default values
AUltimatePawn::AUltimatePawn()
{
// 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;
FloatingPawnMovement = CreateAbstractDefaultSubobject<UFloatingPawnMovement>("PawnMovement");
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent");
CameraArm = CreateDefaultSubobject<USpringArmComponent>("CameraSpringArm");
CameraArm->SetupAttachment(StaticMesh);
CameraArm->TargetArmLength = 500.f;
Camera = CreateAbstractDefaultSubobject<UCameraComponent>("CameraComponent");
Camera->SetupAttachment(CameraArm);
SetRootComponent(StaticMesh);
bUseControllerRotationYaw = true;
bUseControllerRotationPitch = true;
}
// Called when the game starts or when spawned
void AUltimatePawn::BeginPlay()
{
Super::BeginPlay();
}
Finally,
UltimatePawn.h :
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "UltimatePawn.generated.h"
UCLASS()
class ULTIMATE_API AUltimatePawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AUltimatePawn();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
void MoveFoward(float Amount);
void MoveRight(float Amount);
void Turn(float Amount);
void LookUp(float Amount);
void Shoot();
UPROPERTY(EditAnywhere, Category = "Shooting")
TSubclassOf<class ABullet> BulletClass;
class UFloatingPawnMovement* FloatingPawnMovement;
UPROPERTY(EditAnywhere, Category = "Components")
UStaticMeshComponent* StaticMesh;
UPROPERTY(EditAnywhere, Category = "Components")
class UCameraComponent* Camera;
UPROPERTY(EditAnywhere, Category = "Components")
class USpringArmComponent* CameraArm;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};