Check for nearest target and move it it on button press

Hi,

I’ve been seriously struggling with the same issue for days now. I’m creating a top down survival game for mobile (mostly for learning purposes) and I’m implementing a harvest resource/interact system., but I have some issues getting it to focus on the actors. I’ve been searching hundreds of tutorials but they are all using FPS and a system where you mouse at the actor you want to attack/harvest (chop tree) with a line trace.

What I want to do is pictured below:

  • The Player checks in a radius around it for nearest actor which is interact-able (Resource/Enemy) and highlights it (spawn circle underneath), if the INTERACT button on screen is pressed, the Player will move to highlighted actor (nearest), when under a given vector length, it will set “PlayerIsHarvesting” and start animations+attacking resource (tree), which on death will give wood.

Same goes for Attack, but instead to Enemy actor and uses a separate button.

See EPIC drawing below:

I have tried accomplishing this goal first by using an invisible collission sphere on my Character and use OnBeginOverlap. Couldn’t get it to work. Then I tried LineTraceByObject (Sphere). Can’t get it to work. Tried other line trace methods and even went to the horrible GetAllActors input. Still can’t get it to work, so I must admit defeat. _

Can someone please help me?

See BP script below:

Great, cant edit my post:

Here’s the blueprint overview of what I last tested

Thanks, my resources are blueprints. This sounds like a correct way to do this. I just have no idea how to set it up. I basically just need my character to run to the three and attack it.

Hi

I might be on the wrong path, but I will assume the resources are blueprints.

Could you not run a Blueprint Interface to find out which resource is in range, find that location and move towards it.

I use this in a different way, I have stores in my mobile game, and when I walk up to one, I click on it and the interface works out which store it is and activates that blueprint, opening widgets and alike.

This is using line trace under cursor, but you could use other line traces or collision sphere to do this.

This is only if your resources are Blueprints though.

Enemy should just work on begin overlap or Pawn Sensing to activate.

Hope this will solve you issue

Regards
Narg

ok, give me a few I can show you how to setup a BP interface.

I can help you with the “nearest” actor part of your problem. I created a custom node that does exactly that, you input the actor you want to check the distance from (your player in this case) and feed it a reference of “actors” to sort (your resource actors/enemies) and it will give you a sorted array with the nearest actor first. You would then simply use a “Get” node and retrieve index 0. The node looks as below. All you need to do is a sphere trace for objects around your player character to generate the necessary array and the node will take care of the rest :slight_smile:

To Make a BP Interface

  1. Right Click to Make BP → Blueprints → Blueprint Interface

  2. Name it

  3. Open it → It will already have a Function, name it so you know what to name to pull.

Add to all relevant BPs

  1. Open Class Settings in the top menu
  2. Add Interface on Left hand Side ‘Implemented Interfaces’

For begin overlap collision In you BP

  1. Create Event Begin Overlap

  2. From overlap, Out from Other Actor, draw out and call your interface. It will be written - ‘NAME (Message)’

In your Resource BP

  1. Call your BP interface, this time it will be an under “Call Function” in the dropdown menu.
  2. From there you will be able to call your actions, exmaple, create a widget, do a move to, get the Enemy ai to start to attack player.

See below mine, different but same concept.

Interface Outside

266125-interface-1.png

Interface Inside

Inside BP, Class Settings

Calling BP Interface from Begin Overlap

Excuting within your overlapped BP

Regards
Narg