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
AttachToComponentto 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:
-
Got the rifle class from the weapon struct.
-
Spawned the first-person rifle using
SpawnActorFromClasswithAlways Spawn, ignoring collisions. -
Set the rifle’s owner and instigator to
self. -
Stored the spawned actor in a
FPWeaponvariable. -
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:
-
Bound the input action for firing.
-
Created a
Server_FireWeaponcustom event in C++, with inputs for camera location and rotation. -
Stored the weapon reference in C++ (set during the equip event in BP).
-
In
Server_FireWeapon, I checked if the weapon was valid, then triggered the fire logic. -
I performed a
LineTraceByChannel, using:-
Start: Camera location
-
End: Camera location + (Camera rotation forward vector * distance)
-
-
I ignored both the weapon actor and its owner (the character) in the trace to avoid self-hits.
Comments
Post a Comment