Friday, May 15, 2020

Unreal Engine 4 Multiplayer Physics Actor Replication in Blueprints -- Part 1

Yep, probably our longest article title to date.

Multiplayer Network Replication in Unreal Engine is a daunting task. Compared to the "simplicity" of making a single-player game, you basically have to add an extra layer of logic on top of your existing logic. There's also managing and differentiating what should go where among things like player states, games states, and game instances. This guide will show you how to get around some of the more frustrating and less-than-intuitive parts of using UE4 Blueprints to replicate a physics actor pawn in your game.

Using UE4 v4.24

I had originally started working on a small side project about a year ago--a basic multiplayer golf game. Why do I feel the need to make every game work for multiplayer? I often ask myself that same question.

The following example will assume you have a basic understanding of the engine and cover the high points. If you're reading this article, then you should probably watch this YouTube series about UE4 replication. It's humorous, easy to follow, and covers some really important stuff.

You may have already searched for similar answers to this problem. You might've found a forum post that suggested adding a transform variable to your actor, replicating it, and then hooking an event tick to it. Don't do that. Huge waste of network resources. All of the other "helpful" posts suggest looking at the rolling ball template in UE4. What kind of game do you make with that kind of rolling anyway? A Katamari ripoff?

Back to the matter at hand--your main character is a ball that rolls and you want your friends (who also happen to be rolling balls) to see. We don't want it to just roll, we want to hit it with a blunt object, send it careening into other objects where it will bounce around with all kinds of physics and then it can roll some...If it wants to.

Step 1: Create the Pawn

Create a basic pawn class Blueprint (referred to as "BP" from this point on) actor. In the Components section of the BP, set a static mesh (named "BallMesh" in my example) as the root. Attach a Spring Arm component to the ball. Attach a Camera component to the Spring Arm. It should look something like the image below.

Step 2: Component Settings

In the Components section, select the entire BP actor (so you'd click "BP_BallPawn(self) in the example above). Navigate to the "Replication" heading in the Details panel. Make sure that these three radio boxes are checked:
  • Replicate movement
  • Net Load on Client
  • Replicates

Next, select the static mesh component (BallMesh in my example). If you haven't already, set your ball mesh as the static mesh. Under the Physics heading, make sure to click the "Simulate Physics" and "Enable Gravity" radio boxes.

Under the Collision heading, make sure you have collision enabled and the object type to "PhysicsBody."

Finally, and most importantly, under the "Component Replication" heading, make sure to check the Component Replicates radio box. 

Next, select the Spring Arm component (cleverly named "SpringArm" in my example). In the Details panel, under the Camera Settings heading, make sure the "Use Pawn Control Rotation" and "Inherit Yaw" settings are set to true. Make sure "inherit roll" isn't checked or you're going to risk motion sickness the first time you test this out. 

Other things to note: 

  • Make sure the origin for your ball mesh is in the center of the ball, not on the bottom. This will keep the ball from rolling correctly. If you're using Blender for your 3D modeling, you can fix this easily by selecting your ball 3D model in Object Mode, using the Search function (you can pull this up using spacebar if you are using the legacy Blender controls) for Set Origin, and then select "Origin to Center of Mass (Surface)."

  • If you didn't set your own collision for your ball mesh before importing it into UE4, make sure that the collision for your mesh is a sphere. Flat edges don't lend themselves to rolling very well. You can either fix this in Blender by setting your own collision mesh before importing it into UE4 or within the engine itself. If you choose the latter option, double-click your static mesh in the content browser. In the mesh viewport, click the Show drop-down menu and make make sure "Simple Collision" is toggled on. You should see a green outline of your collision mesh (you can barely see it in my example image).
Turn on to view your mesh collision

Your collision should look like this. The green lines indicating the collision
 are hard to see, but they're there if you look close enough.
If your collision looks like a multi-sided, angular mess, then your next step will be removing the existing collision and replacing it. Choose Collision from the top menu and find the "Remove Collision" option. Once the collision is removed, under the same Collision menu, select "Add Sphere Simplified Collision."

Red box first, then blue box.

That's about it for the initial component setup. I had planned to cover this entire topic in one article, but this one is getting lengthy, so I'll finish up with the actual Blueprint nodes portion in Part 2

No comments:

Post a Comment