Have you see this tutorial? Unreal Engine 4 C++ Tutorial Version 4.0.2: Charged Projectile - YouTube
here is my enemy bullet class:
.H
UCLASS()
class TSTEST_API AEnemyWeapon1 : public APawn
{
GENERATED_UCLASS_BODY()
/** Sphere collision component */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Projectile)
class UStaticMeshComponent* ProjectileMesh;
/** Projectile movement component */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement)
class UProjectileMovementComponent* ProjectileMovement;
/** Function to handle the projectile hitting something */
UFUNCTION()
void OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
};
this is my .CPP
#include "GameFramework/ProjectileMovementComponent.h" //You must include this to work.
AEnemyWeapon1::AEnemyWeapon1(const class FObjectInitializer& PCIP)
: Super(PCIP)
{
// Static reference to the mesh to use for the projectile
static ConstructorHelpers::FObjectFinder<UStaticMesh> ProjectileMeshAsset(TEXT("'/Game/Meshes/objects/SM_Projectile.SM_Projectile'"));
// Create mesh component for the projectile sphere
ProjectileMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("ProjectileMesh0"));
ProjectileMesh->SetStaticMesh(ProjectileMeshAsset.Object);
ProjectileMesh->AttachTo(RootComponent);
ProjectileMesh->BodyInstance.SetCollisionProfileName("Projectile"); // Collision profiles are defined in DefaultEngine.ini
ProjectileMesh->OnComponentHit.AddDynamic(this, &AEnemyWeapon1::OnHit); //.AddDynamic(this, &ATStestProjectile::OnHit); // set up a notification for when this component hits something
RootComponent = ProjectileMesh;
// Use a ProjectileMovementComponent to govern this projectile's movement
ProjectileMovement = PCIP.CreateDefaultSubobject<UProjectileMovementComponent>(this, TEXT("ProjectileMovement0"));
ProjectileMovement->UpdatedComponent = ProjectileMesh;
ProjectileMovement->InitialSpeed = 500.f;
ProjectileMovement->MaxSpeed = 1000.f;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->bShouldBounce = false;
ProjectileMovement->ProjectileGravityScale = 0.f; // No gravity
// Die after 3 seconds by default
InitialLifeSpan = 3.0f;
}
void AEnemyWeapon1::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
// Only add impulse and destroy projectile if we hit a physics
if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL) && OtherComp->IsSimulatingPhysics())
{
OtherComp->AddImpulseAtLocation(GetVelocity() * 20.0f, GetActorLocation());
}
Destroy();
}
this is my enemy that i’m attaching the bullet blueprint to:
#include "ParticleDefinitions.h"
#include "EnemyWeapon1.h" //must include bullet header file to work
#include "Engine.h" // get world functions
......
//in enemy header file i add:
void Fire();
/* How fast the weapon will fire */
UPROPERTY(Category = Gameplay, EditAnywhere, BlueprintReadWrite)
float FireRate;
/* Handler for the fire timer expiry */
void ShotTimerExpired();
bool bFire;
enemy CPP :
void AZplane::Fire()
{
if (bFire == true)
{
const FRotator FirRot = PlaneComponent->GetSocketRotation("gunsock");
// Spawn projectile at an offset from this pawn
const FVector SpawnLocation = PlaneComponent->GetSocketLocation("gunsock"); // +FireRotation.RotateVector(GunOffset); //GetActorLocation()
UWorld* const World = ();
//if (World != NULL)
//{
// spawn the projectile
World->SpawnActor<AEnemyWeapon1>(SpawnLocation, FirRot);
()->GetTimerManager().SetTimer(this, &AZplane::ShotTimerExpired, FireRate);
bFire = false;
}
}
void AZplane::ShotTimerExpired()
{
bFire = true;
}
Build it and load up your editor
After that, I create a Blueprint with your bullet class (search for your class under “all classes”
and then selet it to connect the C++ to your blueprint