Creating FPS Multiplayer Prototype

Gameplay Updates


Spawning Attackers and Defenders – Setting Up Weapon Equip Logic

When setting up player spawns for attackers and defenders, I wanted to ensure that each character equips a rifle if they don’t already have one. So, I first checked if the rifle was present, and if not, I moved forward with equipping it.

Equipping the Rifle (Third-Person Mesh)

To handle rifle equipping, I created a BlueprintImplementableEvent in C++ called EquipRifle within the TP_BaseCharacterWeapon class. I made it both BlueprintCallable and BlueprintImplementableEvent so I could call it from C++ and define the logic in Blueprints.

In this event, I:

  • Turned off physics and gravity on the rifle.

  • Disabled collision for both the rifle’s mesh and its sphere collider.

  • Attached the rifle to a socket on the third-person mesh.

Communicating with the Character (Using Interfaces)

To notify the third-person character to equip the rifle, I added an EquipRifle function to my Blueprint Interface. I passed in the weapon struct and the third-person weapon as inputs. In the function implementation:

  • I used AttachToComponent to attach the weapon to the mesh.

  • I added a socket to the third-person character skeleton and set the weapon’s owner before attachment.

First-Person Animation & Weapon Handling

To handle first-person animation blending between knife and rifle:

  • I used a Blend Poses by Bool node in the FP Anim BP.

  • I created an interface function to update the blend pose based on an integer (NewBlendIndex).

  • In the BP character, I got a reference to the FP Anim BP and stored it as a variable during BeginPlay.

Attaching the First-Person Rifle

Once the animation blending was complete, I:

  1. Got the rifle class from the weapon struct.

  2. Spawned the first-person rifle using SpawnActorFromClass with Always Spawn, ignoring collisions.

  3. Set the rifle’s owner and instigator to self.

  4. Stored the spawned actor in a FPWeapon variable.

  5. Attached the weapon to the FP mesh using AttachToComponent, specifying the correct socket.

Fire Logic Setup

Here’s how I set up the firing system:

  1. Bound the input action for firing.

  2. Created a Server_FireWeapon custom event in C++, with inputs for camera location and rotation.

  3. Stored the weapon reference in C++ (set during the equip event in BP).

  4. In Server_FireWeapon, I checked if the weapon was valid, then triggered the fire logic.

  5. I performed a LineTraceByChannel, using:

    • Start: Camera location

    • End: Camera location + (Camera rotation forward vector * distance)

  6. I ignored both the weapon actor and its owner (the character) in the trace to avoid self-hits.


Comments