Hi all,
So I have split-screen 2 player game I’m working on for a Game Jam where players can blast each other with a force that applies a velocity. (Using a sphereTraceForObjects).
My issue: I am trying to create a Swap mechanic using the sphereTrace that on hit, swaps the world locations and relative velocities of player 1 and player 2.
E.g. P1 blasts P2 → P2 is launched into air relative to direction of blast → P2 in mid-air hits P1 with a Swap → P1 and P2 location and velocity Swap → P1 is now being launched with P2’s velocity.
I can attach some images of my blueprint so far later on today to help but any advice anyone has is greatly appreciated!
Cheers,
Joe.
As for the basics, you’re going to be using the Get Actor Location and the Set Actor Location nodes to swap your two players’ locations. On the set location nodes, you’ll want to set the Teleport parameter to true to ensure you don’t cause any sweeping collisions as each player is transported from one place to the other.
You’ll also need the Get Velocity node to get each pawn’s velocities, which you will swap by using the Set Physics Linear Velocity node. (Note that the Set Physics Linear Velocity node requires a component reference, such as your pawn’s mesh component; in other words, you can’t just feed in an actor reference to that node.)
You might also want to consider disabling your pawns’ collisions during the swap to prevent the pawns from momentarily colliding with each other when one is moved into the other’s position. This might not be necessary, but it’s something to think about if you start noticing weird behavior after the swap occurs.
Hopefully that’s enough to get you started, but let me know if you could use some more tips!
Thanks a lot for the help! I was going along these lines but I think it is just my ordering that is causing issues. I will have a tinker around and see if I can fix it. If not I’ll post up my blueprint for review Appreciate it!
So this is what I have so far. There is a lot of inconsistency with swapping locations but sometimes it works. I think the collision is a big problem right now and I have been unsuccessful implementing something.
Any tips or advice you can offer is greatly appreciated.
Note: I appreciate I should collapse a lot of this into separate functions
I didn’t get a chance to make my own blueprint graph last night to test anything, but I hope to get a chance to do so this evening. In the mean time, thanks for bearing with my explanations without accompanying screenshots!
As for the setup you shared in your screenshot, it looks like it should work, except you have a couple of options that should probably be changed to improve the behavior. Changing these might fix the inconsistency you’re noticing.
- You should uncheck the
Sweep
parameter on the Set World Location nodes. This option causes the actor to “move along” the path between its current location and the destination, triggering overlaps along the way and stopping short if blocked by something. You just want your pawns to instantly change places, so you don’t need this option.
- You should uncheck the
Add to Current
parameter on the Set Physics Linear Velocity nodes. With this on, your pawns are adding each other’s velocities to their own, instead of completely swapping velocities. You definitely want this option off.
Once you change those parameters, if you’re still getting weird behavior, I think it might be due to your pawns colliding with each other after the first pawn is moved into the second’s place, and before the second pawn is moved to the first’s place. If that is your issue, you could avoid the problem by disabling collision on both pawns at the start of your script, and then re-enabling collisions at the back of your script (only after both movements have been completed).
Hopefully this helps you get closer to what you’re looking for!
Thanks a lot for the explanations, very useful and easy to understand!
I’ve managed to set-up the collision enable/disable so will try out your suggestions and post back here with my findings. Thanks again!
The main problem in the blueprint was I wasn’t storing the players initial world location for it to be used in the Set Location of player 2.
Instead player 2 wasn’t moving because the location plugged into it was Player 1’s new position.
I have it all working now! Thanks so much for the advice, it helped a lot. If anyone is interested to see the Blueprint, let me know, otherwise this can be closed.