Unreal engine 4 How to make an artillery strike

So, I want to make an artillery strike in my game. I have tried but now i give up and ask:
How do I build a mortar/artillery strike, where bombs (5-15 of them) is exploding randomly in certain area. And it must loop after about every 1 minute. The “bombs” doesn’t have to “fall from the sky”, just only explode top of the ground.

Here is example, what i have done (It’s wrong, I know. It doesn’t even kill the player/character :slight_smile: )

I actually happen to have some C++ code for this, which could be of use to you.
This is the end result: - YouTube
(ignore the commentary - it was for a uni project =p)
I’m not so great at BP, but I don’t mind helping you to convert it.

I know you don’t need to actually have the artillery falling from the sky, but take a look at how I generate a random target.

I don’t have enough characters to write all of the code in one comment, but I’ll start with the most relevant part, and if you want more, I can add it later.

So, the simple part is how I get a random location. BoundingBox is a UBoxComponent, which acts as the target area.

FVector ARegion::GetRandomPointInVolume() {
	if (BoundingBox != NULL) {
		FVector SpawnOrigin = BoundingBox->Bounds.Origin;
		FVector SpawnExtent = BoundingBox->Bounds.BoxExtent;
		return UKismetMathLibrary::RandomPointInBoundingBox(SpawnOrigin, SpawnExtent);
	}
	return FVector();
}

Then, spawning the shell and calculating velocity.

void ABattery::FireAtLocationLogic_Implementation(FVector startPos) {

		FVector TargetPosition = currentTargetRegion->GetRandomPointInVolume();

		const FRotator newrot = UKismetMathLibrary::FindLookAtRotation(startPos, TargetPosition);
		AShell* Proj = World->SpawnActor<AShell>(ProjectileBP, startPos, FRotator(0, newrot.Yaw, 0), SpawnParams);
		Proj->ProjectileComponent->SetVelocityInLocalSpace(FVector(0, 0, 0));
		const float Gravity = World->GetGravityZ() * -1;
		const float Theta = (40 * PI / 180); 

		FVector dir = TargetPosition - startPos; //direction
		float Sz = dir.Z; // Height difference
		dir.Z = 0; // Remove hight from direction
		float Sx = dir.Size(); // Distance

		const float V = (Sx / cos(Theta)) * FMath::Sqrt((Gravity * 1) / (2 * (Sx * tan(Theta) - Sz)));
		FVector VelocityOutput = FVector(V*cos(Theta), 0, V*sin(Theta));

		Proj->SetFiringVelocityAndTargetLoc(VelocityOutput);
}

[continued here due to lack of characters]

I’m not sure if this next part will mean much to you, but this is how I added the delay to the guns firing.

void ABattery::DelayFiringOrder(int32 gunIndex) {
	float delay = FMath::RandRange(0.1f, maxDelayBetweenRounds);

	switch (gunIndex) {
	case 0:
		GetWorld()->GetTimerManager().SetTimer(GunOneTimerHandle, GunOneTimerDel, timeBetweenVolleys, true);
		DelayTimerDel.BindUFunction(this, FName("DelayFiringOrder"), 1);
		GetWorld()->GetTimerManager().SetTimer(DelayTimerHandle, DelayTimerDel, delay, false);
		break;
	case 1:
		GetWorld()->GetTimerManager().SetTimer(GunTwoTimerHandle, GunTwoTimerDel, timeBetweenVolleys, true);
		DelayTimerDel.BindUFunction(this, FName("DelayFiringOrder"), 2);
		GetWorld()->GetTimerManager().SetTimer(DelayTimerHandle, DelayTimerDel, delay, false);
		break;
	case 2:
		GetWorld()->GetTimerManager().SetTimer(GunThreeTimerHandle, GunThreeTimerDel, timeBetweenVolleys, true);
		GetWorld()->GetTimerManager().SetTimer(VolleyTimerHandle, this, &ABattery::EndFiringOrder, ((timeBetweenVolleys * numberOfVolleys) + 1), false);
		break;
	}
}

In UE4 C++, the TimerHandle is the typically method fro adding delay.
I just set a timer looping that repeatedly calls a function until EndFiringOrder() is called at which point, all of the loops are stopped. The main advantage to this was that I was able to add a random bit of delay between each gun firing.

In blueprint, perhaps you could try a for loop with a delay. Then, you could loop for as many rounds as you want firing. Take a look at some of the answers here: https://answers.unrealengine.com/questions/50642/question-how-do-i-add-a-delay-to-each-iteration-of.html

Again, I don’t mind helping you set something up in BP, if what I’ve said already doesn’t help you. ^^

Thanks! I’m trying to solve this out and if I get it, I’ll publish “howto” video on Youtube, because we’re not the only one who is trying to figure this out (I believe).

That last part would be nice, so the enemy team can realize to get cover, before artillery hits them.

I am ready to receive all the help, I would appreciate it!

So, I took a stab at making my code in BP form.
Here is the result so far: Screen capture - a60f4a36aaafd1b99593abfc895c5263 - Gyazo

It currently just fires once on begin play, but it calculates the velocity based upon the gun’s barrel and the randomly generated target location.

Here’s what I have so far…
I have 4 blueprints: Battery, Gun, Shell and Target Zone.

The Battery stores an array of guns and will handle stopping and starting firing, whilst also telling the guns where to shoot. The guns are passed a target location, and then using that calculate a velocity and spawn a shell, applying that to the shell’s projectile movement component.

The Zone has a Box Collision Component, and the gun has a Scene Component(GunBarrel). You could use an arrow for this, if you preferred.

The calculating part is a bit more complex. It currently works with fixed angle launch - which means that the angle of launch will not change - only the velocity. Should be fine for your needs.

I can probably share the project with you later. Still got to look into that timing, as well as projectile collision/ detonation. Hope this is at all helpful.

One more thing, before I give up for today:

Adding this to the projectile BP will make it rotate to face its direction of travel.

Last post, honest!

I made a custom macro using one of the suggestions on that link I sent earlier.

Thanks! Sorry for waiting the answer, I were on a holiday. I see that you know what to do and if I have done that all by myself, it would have taken more than a week (im a newbie).
It looks like it could work as I imagined it. Only the cannon shell’s have to be invisible but I guess you can mark them as “invisible ingame”.

Have to try that and make a video in Youtube. Thanks again!

No problem at all. If you can get a good system set up, then the UE4 community will appreciate a video demonstrating your approach.

If you don’t need the shells to be visible, then you can simply spawn an explosion(particles, hitbox etc.) at the random location.
Really, you wont need the cannons either - I just copied my own example in Blueprint form, so much of what I’ve done isn’t even that useful to you. ^^’

The main parts you’ll probably be interested in are the generating a random point ina volume and the Loop With Delay, followed by spawning an explosion blueprint which can check for collisions and display a particle effect.

Good luck with it, and feel free to ask for more help.

Yes, I dont need artillery shells to be visible. In the video above is more what im looking for: Sniper Revolvers, Precise Artillery, and Sharp Knives - Verdun Gameplay - YouTube
You have done enough for me and I appreciate that! Thanks again!

I will inform you (if this doesn’t get locked) in here, when the job is done :slight_smile:

I can see the shells in that video :stuck_out_tongue_winking_eye:

You’re welcome. I hope just one part of it helps, and if not it might be useful to someone else or even myself in the future.

yeah, sorry. I remember wrong and posted video without watching it again :smiley: So can you post pictures of which BP include what code :slight_smile: I’m a bit confused here right now (if you still have them somewhere) :stuck_out_tongue: