In part one, we did the initial setup. Now that the boring part is out of the way, we can dive into the good stuff. In the second half of this tutorial, I'm going to show you how to add some force to your ball to make it move and, more importantly, how to replicate that movement.
The logic in this example is going to be pretty straightforward: we're going to find where your camera is pointing and knock the ball straight ahead. We'll do this with a combination of "things" happening on the server and things just happening locally.
I'm going to assume you've got at least a cursory amount of knowledge on creating custom events and replication. Otherwise, this tutorial isn't even going to begin to scratch the surface of helping you down this difficult path.
Step 1:
In your Construction Script, drag a pin from the Construction Script node and create a Set Use CCD node. Make sure to check the box next to "In Use CCD." Set the physics actor as the target of the Set Use CCD Node. This will ensure that your ball will bounce off walls and other collision meshes instead of shooting straight through them.
I actually created a whole tutorial around Step 1.
Step 2:
Now we need to add some controls to let you twist your camera around your ball.
Back in the Event Graph, create a Mouse X node and drag out the white execution pin to create an Add Controller Yaw Input node. Connect the "Axis Value" of the Mouse X node to the "Val" input of the Add Controller Yaw Input node.
Now moving your mouse left and right should make you twist around your ball. You might want to go into your blueprint editor and change the rotation of your Spring Arm component so that the camera is elevated above the ball, but I'll leave that to personal preference.
Step 3:
Create a float variable. In the example, I've named mine "LaunchSpeed." Set the replication value to "replicated." Go ahead and compile the blueprint so that we can set a default launch speed. Set the default value to 100. Depending on the size of your ball, you may need to adjust this value.
Create a Custom Event. Name it something memorable, like "SR_LaunchBall." We're going to be using this to get the server to launch our ball. In the Details panel of SR_Launchball, set the Replicates value to "Run on Server." At the bottom of the Details panel, hit the little plus sign to add an input. I've named mine "InRot." Set the variable type to Rotator.
Step 4:
Right click your Event Graph and create a node to call your version of the SR_LaunchBall event, above.
Right click the graph again and add a keyboard or mouse event. This is the button you'll be using to launch the ball. In my example, I've used Space Bar...because we're going to knock this ball into space!. Just kidding. It should be very manageable.
Connect the execution output pin of Space Bar to the input of SR_LaunchBall.
From your components panel, drag your Camera component onto your graph. It should automatically make this into a "get" node. Drag the pin out from Camera and use the Context Menu to create a Get World Rotation node. Connect the output of Get World Rotation to the "InRot" of the SR_LaunchBall Event/Function.
So when the client presses space bar, you'll send the local camera world rotation to the server and tell the server to fire the the SR_LaunchBall event.
Step 5: (The result in typing this one out was kind of confusing. You'll probably need to just rely on the example image)
Find your custom SR_LaunchBall event you created in Step 3. Drag the rotator wire from "InRot" and use the Context Menu to create a Get Forward Vector node. From the Return Value of Get Forward Vector, use the context menu to break that vector. from there you'll be multiplying the X and Y values by the launch speed (leave the Z alone) and making a new vector from this data.
Drag an execution pin from SR_Launchball and create an Add Impulse node. Attach the output of the Make Vector node to the "Impulse" input on the Add Impulse node.
From your actor components, drag your ball physics actor mesh onto your graph and attach that as the Target of the Add Impulse node.
And that should be it!
Note:
- In your game, you'll probably want to set some logic to allow the player to set how fast/hard the ball is launched. Pay attention to how we got the local client's camera rotation data to the server in SR_LaunchBall, above.
- One concept that took me the longest to learn was that "mixing" client and server variables is a bad idea (which gets even more confusing if you're the server, but that's another rabbit hole I won't be attempting to enter).
No comments:
Post a Comment