Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Swapping relative velocities for 2 actors

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!



Product Version: UE 4.20
more ▼

asked Jan 22 '19 at 01:45 PM in Blueprint Scripting

avatar image

1 2 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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!

more ▼

answered Jan 22 '19 at 06:41 PM

avatar image

1.2k 4 5 8

avatar image Joe1Edgeley Jan 22 '19 at 10:09 PM

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!

avatar image Joe1Edgeley Jan 23 '19 at 01:38 AM

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.alt text

swap-blueprint.jpg (246.5 kB)
avatar image Joe1Edgeley Jan 23 '19 at 01:40 PM

Note: I appreciate I should collapse a lot of this into separate functions

avatar image the_batch Jan 23 '19 at 03:45 PM

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!

avatar image Joe1Edgeley Jan 23 '19 at 04:01 PM

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!

avatar image Joe1Edgeley Jan 30 '19 at 02:56 PM

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. :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question