Hi everyone,
I am following the projectile tutorial from UE4 as I wanted to try to better understand the best way to implement a basic projectile
However, as I have experience with other parts of UE4 and integrating C++ and Blueprints, I decided to add materials and meshes via the blueprint system directly to the static mesh created as a defaultsubobject in code, rather than using the constructorhelpers (as I do not want to use these later on).
However, when I shoot my projectiles in game, they shoot fine, and when I pause the game I can see them in the world outliner, and eject from my character to focus in on them.
They do not, however, have a mesh or material, so are invisible in game.
Here is the blueprint screen with their mesh component and materials implemented, as well as what they should look like.
I have made sure ‘visible’ is always ticked for all components on this (even the collision component, just for testing), and that hidden in game is also not ticked.
The header code is here.
.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/SphereComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "FPSProjectile.generated.h"
UCLASS()
class FPSGAME_API AFPSProjectile : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFPSProjectile();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
//Spherical collision component
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
USphereComponent* CollisionComponent;
//Projectile Movement Component
UPROPERTY(VisibleAnywhere, Category = Movement)
UProjectileMovementComponent* ProjectileMovementComponent;
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
UStaticMeshComponent* ProjectileMeshComponent;
//Function to intiialize projectile velocity in the shoot direction
void FireInDirection(const FVector& ShootDirection);
};
The constructor code is here.
cpp:
AFPSProjectile::AFPSProjectile()
{
// 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;
bReplicates = true;
if (!RootComponent)
{
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("ProjectileSceneComponent"));
}
if (!CollisionComponent)
{
//Use sphere as simple collision representation
CollisionComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
//Set collision radius
CollisionComponent->InitSphereRadius(15.0f);
//Set root component to be collision component as this will be the driven component by the sim
RootComponent = CollisionComponent;
}
if (!ProjectileMovementComponent)
{
//Use this component to drive projectile movement
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
ProjectileMovementComponent->SetUpdatedComponent(CollisionComponent);
ProjectileMovementComponent->InitialSpeed = 3000.0f;
ProjectileMovementComponent->MaxSpeed = 3000.0f;
ProjectileMovementComponent->bRotationFollowsVelocity = true;
ProjectileMovementComponent->bShouldBounce = false;
ProjectileMovementComponent->Bounciness = 0.3f;
ProjectileMovementComponent->ProjectileGravityScale = 0.0f;
}
if (!ProjectileMeshComponent)
{
ProjectileMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ProjectileMeshComponent"));
}
ProjectileMeshComponent->SetupAttachment(RootComponent);
InitialLifeSpan = 3.0f;
}
...
}
I am very confused about this, so any help would be appreciated. Thank you