I am trying to develop a system for firing projectile from my plasma pistol but after doing all the necessary work i am stuck on this bug where my projectile spawn 10 cm from my gun’s muzzle socket and the projectile remain stationary in the air, how can i fix that
projectile.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Components/SphereComponent.h"
#include "NiagaraComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraSystem.h"
#include "PlasmaProjectile.generated.h"
UCLASS()
class MYSIDESCROLLER_API APlasmaProjectile : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APlasmaProjectile();
UPROPERTY(VisibleAnywhere)
USphereComponent* CollisionComponent;
UPROPERTY(VisibleAnywhere)
UProjectileMovementComponent* ProjectileMovement;
UPROPERTY(VisibleAnywhere)
UNiagaraComponent* PlasmaBall;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Effects")
class UNiagaraSystem* ExplosionEffect;
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComp,AActor* OtherActor,UPrimitiveComponent* OtherComp,FVector NormalImpulse,const FHitResult& Hit);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
projectile.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "PlasmaProjectile.h"
// Sets default values
APlasmaProjectile::APlasmaProjectile()
{
// 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;
CollisionComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
CollisionComponent->InitSphereRadius(15.0f);
RootComponent = CollisionComponent;
PlasmaBall = CreateDefaultSubobject<UNiagaraComponent>(TEXT("PlasmaBall"));
PlasmaBall->SetupAttachment(RootComponent);
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComponent"));
ProjectileMovement->UpdatedComponent = CollisionComponent;
ProjectileMovement->InitialSpeed = 4000.f;
ProjectileMovement->MaxSpeed = 4000.f;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->ProjectileGravityScale = 0.0f;
}
// Called when the game starts or when spawned
void APlasmaProjectile::BeginPlay()
{
Super::BeginPlay();
if (CollisionComponent) {
CollisionComponent->OnComponentHit.AddDynamic(this, &APlasmaProjectile::OnHit);
}
}
// Called every frame
void APlasmaProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
DrawDebugSphere(GetWorld(), GetActorLocation(), 10.f, 8, FColor::Red, false, 1.0f);
FVector CurrentLoc = GetActorLocation();
UE_LOG(LogTemp, Warning, TEXT("next location: %s"), *CurrentLoc.ToString());
}
void APlasmaProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if (OtherActor && (OtherActor != this) && OtherComp)
{
// 1. Spawn explosion at the EXACT spot we touched
UNiagaraFunctionLibrary::SpawnSystemAtLocation(GetWorld(), ExplosionEffect, Hit.ImpactPoint, Hit.ImpactNormal.Rotation());
// 2. Destroy the projectile
Destroy();
}
}
character.cpp
void ASpaceCatCharacter::fire()
{
if (PlasmaProjectileClass)
{
FVector MuzzleLocation = PlasmaPistol->GetSocketLocation(TEXT("Muzzle"));
FRotator MuzzleRotation = PlasmaPistol->GetSocketRotation(TEXT("Muzzle"));
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = GetInstigator();
GetWorld()->SpawnActor<APlasmaProjectile>(PlasmaProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("PlasmaProjectileClass is NOT set in the Character Blueprint!"));
}
}


