Send object to specific character depending on view

Hi all, I’m trying to build a system where a player will pass the ball to another player depending on whether they are in their line of sight. Currently I’m using the player with the ball’s view and comparing it to the current location of all the players on the team but I’m not sure how I use what I’m getting to pass the ball to them.

The “Get Actor Eyes View Point” function is part of the AI PawnSensing system… it doesn’t give you the point that the Actor is looking at, which seems to be how you’re using it.

1 Like

You could use traces to find the actual point that the Actor is “looking at”, but I think you’d be best off by comparing the vector from the PlayerWithBall to the other player in world space with the GetForwardVector result of the PlayerWithBall.

1 Like

How would I then go about selecting the desired player from the array?

Thanks

I was thinking something like this, probably as a function with local variables.

So, the FriendlyTeamPlayers array is just the result of GetAllActorsOfClass (technically you should to call that function as little as possible, it’s expensive)

The Cast To CameraComponent node should be replaced with a reference to PlayerWithBall’s camera component… the goal there is just to get a unit vector of the camera’s view direction.

We subtract PlayerWithBall’s location from the FriendlyTeamPlayer’s location and normalize the result. This gives us a unit vector pointing in the direction from PlayerWithBall to the FriendlyTeamPlayer.

Next, we take the dot product of the two vectors. When being used with unit vectors, it can give us an idea of how similar the vectors are. It will return 0 if they are perpendicular, -1 if they are opposite, and 1 if they are the same. So, the closer the dot product is to 1, the more similar the vectors are.

The first CompareFloat node is there to make sure that the FriendlyTeamPlayer is at least sort of in our direction. I just came up with 0.4, but you’ll want to tune that value. Decreasing it will make the check more sensitive, increasing it will make the check less sensitive.

The second CompareFloat node is there to check if this FriendlyTeamPlayer is the one that’s the most in our direction. Since MaxDotProduct starts at -1, anything will be greater. If MaxDotProduct gets beat, then the dot product that was greater gets set to be the new MaxDotProduct, and we keep track of which FriendlyTeamPlayer that was in the “Best Pick” variable.

The loop repeats, going through all of them to see which FriendlyTeamPlayer is the most in the direction of the view of the PlayerWithBall, and that FriendlyTeamPlayer is stored in “Best Pick”. As mentioned in the comment, it may be unset if none were greater.

Some things to consider:

  1. My Blueprint doesn’t take into account direction. You may want to change that.
  2. Technically you could have BestPick be an integer storing the Array Index of the selected FriendlyTeamPlayer; it’d be better practice.
  3. As mentioned, this should probably be a function, or at least you must make sure MaxDotProduct and BestPick are reset every time this is run.

Hope this helps :wink: