SpawnEmitterAtLocation Doesn´t work

I’m doing the last part of the course in UE5, I know I could have problems and this is one of them. My ImpactEffect doesn’t show, I Know that my chanel is working because the debug point works, All the code is the same as in the lecture of the lesson but I don’t know what to do. Anyone here has a similar error?
Image of my code:

video example:
https://drive.google.com/file/d/1VQmneMJeH5wzUmshzA7TEpn12uPUwIcu/view?usp=sharing

1 Like

Please share the full code.

Gun.cpp

#include "Gun.h"
#include "Components/SkeletalMeshComponent.h"
#include "kismet/GameplayStatics.h"
#include "DrawDebugHelpers.h"
// Sets default values
AGun::AGun()
{
 	// 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;
	Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
	RootComponent = Root;

	mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Gun"));
	mesh->SetupAttachment(Root);

}

void AGun::PullTrigger()
{
	UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, mesh, TEXT("MuzzleFlashSocket"));

	APawn* OwnerPawn = Cast<APawn>(GetOwner());
	if (OwnerPawn == nullptr) return;

	AController* OwnerController = OwnerPawn->GetController();
	if (OwnerController == nullptr) return;

	FVector Location;
	FRotator Rotation;
	OwnerController->GetPlayerViewPoint(Location, Rotation);

	FVector End = Location + Rotation.Vector() * MaxRange;

	FHitResult Hit;

	bool bSuccess = GetWorld()->LineTraceSingleByChannel(Hit, Location, End, ECollisionChannel::ECC_GameTraceChannel1);
	if (bSuccess)
	{
		FVector ShotDirection = -Rotation.Vector();
		UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, Hit.Location, ShotDirection.Rotation());
	}
	
}

// Called when the game starts or when spawned
void AGun::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void AGun::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

Gin.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Gun.generated.h"


UCLASS()
class SIMPLESHOOTER_API AGun : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AGun();

	void PullTrigger();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

private:
	UPROPERTY(VisibleAnywhere)
	USceneComponent* Root;

	UPROPERTY(VisibleAnywhere)
	USkeletalMeshComponent* mesh;

	UPROPERTY(EditAnywhere)
	UParticleSystem *MuzzleFlash;

	UPROPERTY(EditAnywhere)
	UParticleSystem *ImpactEffect;

	UPROPERTY(EditAnywhere)
	float MaxRange = 1000;
};

Your code looks fine. Maybe something else is at fault. Zip up your game project file, upload it on any cloud storage platform (Google Drive), and share it with us. I am not a professional, but I will try my best.

And please use File > Zip Project within Unreal so unneeded files are excluded.

Is the code reaching here? Have you tried adding log statements? Did you set the owner of the Gun?

I am assuming that if the owner is not set, then the debug point should not work because he wrote this line in his code: - if (OwnerPawn == nullptr) return;

I am not sure. Maybe I am wrong.

Zip of my project
https://drive.google.com/file/d/1CLm3aSTiQ9_6Fx2FTSv_Z7eY2xy-xoHt/view?usp=sharing

1 Like

You are passing the 1st parameter wrong.

  1. Make sure to disable live coding and compile your code using SimpleShooterEditor Win64 Development Build. If you use live coding, then the impact particle will most likely disappear from BP_Gun every time.
  2. Replace GetWorld() with this. The first parameter is asking for const UObject* WorldContextObject rather than Const UWorld* InWorld.
  3. And also, after compiling your code, go to BP_Gun and make sure the impact particle is still assigned or not.

These steps will resolve your issue.
Good luck on your next adventure.

I use that parameters because I remember using the same function in the toon tank section, but it doesn’t work either. You can see in the code i share earlier that I’m not using “this”

1 Like

Just use below code instead of your:-
UGameplayStatics::SpawnEmitterAtLocation(this, ImpactEffect, Hit.Location, ShotDirection.Rotation());

The Same, Its not working

Do you have discord?

yeah

give me your Id i will send you request

ArturoCruz#8003

The solution was to verify the Unreal Engine Files.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms