Download

Get Distance From One Player to an NPC?

Hey guys. So I’ve created an NPC in my FPS game, and I’m wondering how I can get the distance between the two of them. To create the NPC, I just copy/pasted the FirstPersonCharacter blueprint and changed some stuff from there (like the player model, etc.). Since the NPC and player are in two different blueprints, how can I get the distance between the two? I also need to access some variables from one blueprint in another, and have no idea how to do that. Could someone tell me how to do this, if possible? Thanks!

EDIT: Also, how would I check when you collide with the NPC? I’ve been trying to figure that out for a while now…

To get distance between 2 actors you can subtract both actor’s vector using “get actor location” then store it into a vector length float.

To acces variables from one bp to another you need to use “cast to”.

Where wold I put the “Get actor location”? Which BP? Also, how would I do something when the NPC collides with the player?

Here’s what I’ve got so far, and it’s not working. Keep in mind I’m rather new to UE4, so I’m probably doing things WAY wrong. Also, I need an Object for the cast, and I have no idea what to use.

A cast takes an object of one class, and turns it into a child of that class (as long as it is indeed a child), which lets you access properties and variables specifically for that class. If you are casting to a child of actor, your object needs to be an actor, or parent of the class you are casting to.

If you want to make a 3D game, study vector math from end to end; seriously, you won’t want to skip that.
Nail it before you even start coding anything.

You really want to watch some tutorials on youtube from Epic Games.

When you don’t know how to use Casting, then it’s maybe too early to go fancy already.

To get the Distance between 2 actors, you need a Reference of one actor inside of the other one.

Getting references is not THAT easy. You can get them by Tracing then, getting Overlap events, spawning them and saving the reference etc.

Then, if you have a valid reference (reference variables are empty if you don’t but a real object into them), you can get his ActorLocation as well as the
ActorLocation of the first actor and then subtract them and get the length of the result.

The function you show above also still needs a valid object to be used. The Cast node does not work like that.

forgive the semi/hijack OP.

just jumped into this and thought i’d see the simplest way I could think of for the purposes of learning a bit more myself.

Wouldn’t this be the simplest way to get distance to? it seems to work fine for me, it’s not exact to the last .1 of a unit but surely it would be close enough to keep things simple?

&stc=1&d=1449440580

Yes, but this only works if these 2 objects are already placed in the scene and can only be used for exactly these 2.

So you can’t reuse that for different once dynamically during the play.

ok thanks for the info :slight_smile:

You can use a “find all actors of class” and get the specific 1 you are looking for from that array. Or get it and then check it has a specific tag for example.

But that node is very inefficient and not something that should be used if it can be avoided.

Personally I store my actors in data tables and then I just lookup the row based on their name and then use that actor.