How to make aiming of several turrets to the screen crosshair?

@ben Hi Ben, maybe you could help me (or someone else) . I have a tank with two turrets on the left and right sides of the tank center, what I should change in the code in order to focus them at crosshair point and rotate accordingly?

Right now rotation by default is done by keeping in mind only one socket system (1 turret, 1 barrel), how can I expand that?

Thank you!
Pavel

UPD: How to fire from both of them at the same time or separately (depending on the configuration, e.g. 1click - 2 projectiles spawned at the same time, or 1 click - 1 proj spawned and on the second click another gun fires)?

I could spoon feed you guys the solution, but this sounds like an excellent student exercise to work out between you. Let me know if you’re really stuck.

Maybe @GamedevCala would be a good student to offer the first suggestion if you are willing?

Without being at my gamedevelopment computer, I would either suggest to add the second turret/barrel to the initialise setup and just call your aiming and firing methods in both.

The other way could be to build up an interface and broadcast the new aiming point to any actor that is listening.

For the different fire actions you could use an int variable and count up your shoots and start the firing methods on even / odd counts on the left or right turret.
In Blueprints there is a Flip/Flop node doing this.

Just some brainstorming, because @ben challenged me :wink:

Hello guys,

Thanks for fast replies. What I have tried yesterday (without coding yet):

  1. put the second turret with barrel in the Tank_BP
  2. Call them within an event Begin Play by references for Turret and Barrel
  3. Now EventBeginPlay have nodes: ->
    Turret_001 -> TurretReference - >Barrel_001 -> BarrelReference ->
    ->Turret_002 -> TurretReference - >Barrel_002 -> BarrelReference

The problem here is that Turret_002 is called the last one and during the game only it fires.

I was thinking before about the same idea proposed by @GamedevCala about an interface and broadcasting. I will try to work on that today. How I see it:

  1. I need to have an array of an Actors referenced to the Turret (Turret+Barrel) class(es).
    If I have N Turrets I need to allocate them with an indexes in order to have, for example:
    Turret_001, Turret_003 … Turret_00(N-1) - are on the LEFT side of the tank
    Turret_002, Turret_004 … Turret_00(N) - are on the RIGHT side of the tank

  2. I can force each array to Fire if these Turrets are actually AimAt crosshair position. If they don’t - do nothing.

  3. I can call each Turret from these arrays in order to AimAt each of them (am I right?) and Fire().

I will try that for the time being and write an update if it works.


I think I missed what is “initialise setup” ?? This is done within Reference stuff in Blueprints and the code of Turret.* and Barrel.* ??

So, I am facing the following problem:

I need to get sockets for Turret placing within the C++ code. For this I need to GetStaticMesh->GetSockets (or how it will be called). For sure I will use arrays, so the general task shouldn’t be a problem, but I am stuck with getting sockets within Tank.cpp

Am I right that I need to use:
Tank.h:
UStaticMeshComponent* StaticMesh;

Tank.cpp:
StaticMesh->GetAllSocketNames()

But I was unable to get them

Hello @ben,

so, I am stuck. To spawn Actor I need to have an Actor class, but we are having a StaticMeshComponent for Turret and Barrel.

In my header file I have added the following:

// Local StaticMeshComponent reference for turret spawning
UPROPERTY(Category = Mesh, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = “true”))
class UStaticMeshComponent* StaticMeshComponent;
// Local StaticMesh reference
UStaticMesh* StaticMesh;

// Array of ALL the turrets on the tank
TArray<FName> TurretArray;
// Array of the spawned turrets on the tank
TArray<FName> SpawnedTurretArray;
// Array of the only LEFT side (1, 3, 5...)
TArray<FName> LeftTurretSocketArray;
// Array of the only RIGHT side (2, 4, 5...)
TArray<FName> RightTurretSocketArray;

void GetTurretSockets();
void SpawnTurret(FName _TurretSocketName);
void DestroyTurret();

In c++:
within the constructor:
// Create static mesh component
StaticMeshComponent = CreateDefaultSubobject(TEXT(“Static Mesh”));
StaticMesh = StaticMeshComponent->StaticMesh;

void ATank::GetTurretSockets()
{
TurretArray = StaticMeshComponent->GetAllSocketNames();
// Applying turrets to all sockets on the static mesh
// TODO: more flexible spawning not to ALL sockets, but some special (by name)
for (int i = 0; i < TurretArray.Num(); i++)
{
SpawnTurret(TurretArray[i]);
}
}

void ATank::SpawnTurret(FName _TurretSocketName)
{
// First we want to make sure these values are all zeroed out
FVector SocketLocation;
FTransform SocketTransform;
SocketLocation = FVector(0, 0, 0);
SocketLocation.Rotation() = FRotator(0, 0, 0);

UTankTurret* TurretGun = GetWorld()->SpawnActor<UTankTurret>(Turret, SocketLocation, SocketLocation.Rotation());

// Adding spawned turret to an array
SpawnedTurretArray.Add(TurretGun);

}

I have problem with calling static mesh (I suppose) and with actor spawning. Can you please help me how to solve it?

Could you start by comparing your code to ours by…

  1. Visiting https://github.com/UnrealCourse.
  2. Clicking on the section name.
  3. Clicking XX Commits at the top-left (XX will vary)
  4. Against the video you want, click the <> button on right.
  5. On final page (example) click Download Zip button.

Good luck!

I still can’t spawn several turrets into sockets without re-creating a new Actor classes for turret and barrel.

Turret = GetWorld()->SpawnActor<UShipTurret>();
//SpawnedTurretArray.Add(Turret);
if (Turret != NULL)
{
	//TurretGun->AttachToComponent(this->RootComponent, FAttachmentTransformRules::KeepRelativeTransform, _SocketName);
	Turret->AttachTo(this->GetRootComponent(), _SocketName, EAttachLocation::KeepRelativeOffset);
}

Our turret is UStaticMeshComponent, how to add it to Tank? I was checking the code before and after this lecture up to some point, but still no answer.

I have created an arrays with sockets within the Tank and I can operate with them.

Privacy & Terms