How to make a Rocket explode by itself?

Hello.

My class is Projectile, and I can decide whether that projectile is explosive or not (bExplosive).
The difference between explosive or not, is that when a player is hit, a function called Explode() gets called, and the FHitResult of the Impact is passed.

Now, I have used this class to make the Rocket_Projectile subclass. The Rocket is a projectile with these properties:

  • When a player is hit, it applies **full **damage
  • When an object is hit, it applies **radial **damage

Until here, no problems.

The problem comes because the Rocket has a lifetime. That means, after a certain ammount of time, the Rocket explodes by itself applying a radial damage.

But the Explode() function needs a FHitResult… but I don’t have any HitResult to pass!

In my Explode() class I have some logic to be applied when the Rocket explodes, so it’s really necessary it is called

Any tip?

I think there are a few examples of this in their content, but what I would do is this the rocket actor:

override this…



void AProjectile::NotifyHit(class UPrimitiveComponent* MyComp, class AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit)
{
	Super::NotifyHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation, HitNormal, NormalImpulse, Hit);
	Explode();
}


and override this…



void AProjectile::Destroyed()
{
	if (!bExploded)
	{
		Explode();
	}
}


I already have a method which gets called when the Rocket hits something.

In the BeginPlay() I set a timer:



void AProjectile::BeginPlay()
{

	/* TRIGGER TIMER FOR THE PROJECTILE LIFETIME EXPLOSION */
	FTimerDelegate ExplosionDelegate = FTimerDelegate::CreateLambda(=]()
	{				
		this->Explode( **???** );
	});


	GetWorldTimerManager().SetTimer(ExplosionTimer, ExplosionDelegate, OwnerWeapon->ProjectileConfig.ProjectileLifeTime, false);	
}


Well, the Explode() function requires a FHitResult as parameter. So what do I pass if there’s no HitResult?

It’s like… I want to convert the location of the Rocket Actor, to a HitResult, in order to succeffully call the function.

In my BeginPlay, I call SetLifeSpan(). My Explode(), just explodes where it is at that point in time.

I think I might be missing something. My explode is just cosmetic as the engine takes care of what actually takes the damage.

I don’t know how I can accomplish this. I have a base class Projectile ( below you can see the source code )

Should I make 2 subclasses deriving from Projectile?

I’m confused… I don’t know how I can implement this

This is kind of what I do… I’m not sure if it helps. The example has a dude firing a “rocket”, exploding on contact and doing radial damage. If you hit the guy, he dies from radial damage.

Sounds like a similar setup to the ShooterGame projectiles. Just feed in an empty hit result:



FHitResult NewResult;
Explode(NewResult);


Of course, the Explode function needs to safely handle an empty hit.

Better answer than mine :slight_smile:

You could also do what I do, which is to also pass in a bool that tells the projectile whether to ‘search’ for a nearby surface and use that as the explosion point, doing a sphere ovelap test. I use this when things explode very close to the ground for example, and I still want to leave a decal / explosion particle there.

It seems good for a Rocket. But then my Projectile class can’t be used to represent a Grenade.

The difference between a rocket and a grenade is:
**
When something is hit (anything)**

Rocket: explodes, whatever the surface is

Grenade:
If it’s a player, explodes and applies full damage to him.
If it’s something else (like a wall) just bounce and then fall to the ground.

However, if the grenade is on the ground and hasn’t exploded yet, if a player stumbles over it the greande explodes and applies full damage.

How can I introduce this concept in my game?

Well you’d have to add a sphere component to the grenade (or use an existing one), and when it detects an overlap, cast the actor to see if it’s a character. If it is, explode.

You’ll likely want to inherit from the projectile class to make a grenade.