Announcement

Collapse
No announcement yet.

Nearest enemy to my player character

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Nearest enemy to my player character

    not sure if i should post this here or in the main blueprint help forum

    I have set up a system where as the player approaches the enemy the tempo of my music increases - its a procedural system based on a sequencer approach for the rhythm and random seeds to select the notes of the music. This is working.

    I have a bunch of enemy in the level but the order that the enemy are selected is random or perhaps based on the last one I placed in the level. What I want to do is for the distance to the nearest enemy be selected. The tempo part of my blueprint is working perfectly. I hope this makes sense.

    Can someone help me to refine my blueprint so the nearest enemy to the player is selected? I have tried numerous approaches with no success.

    Attached Files

    #2
    You're checking for the distance to the "AI" actor but you arent really doing anything with it as far as I can tell (aside from comparing distance to some threshold).

    What you want is to check each distance to the one before it and store that to a temporary variable if its smaller, and then get a reference to that actor and set that to another variable. Run the rest of the code at loop completion.
    Need another dev on your UE4 project? Send me a shoutout and I'll lend a hand

    Comment


      #3
      I was playing around with how to do this for one of my projects and I got it sort of working...



      Unfortunately it's doing something weird when there are two actors close to each other so it's not a final solution but maybe it will point you in the right direction?

      Essentially you just need to use the Get Distance To node to determine which of your enemies is closest to the player.
      Last edited by TorQueMoD; 03-10-2018, 10:13 PM.
      -TorQueMoD
      www.torquemod.com

      Comment


        #4
        Thank you guys. I am going to give your suggestions a go and i will report back to you.

        Comment


          #5
          Originally posted by 6ixpool View Post
          You're checking for the distance to the "AI" actor but you arent really doing anything with it as far as I can tell (aside from comparing distance to some threshold).

          What you want is to check each distance to the one before it and store that to a temporary variable if its smaller, and then get a reference to that actor and set that to another variable. Run the rest of the code at loop completion.
          Yes absolutely, i am checking distance from the player character to the enemy character within a threshold of 200, that's when the tempo of my music increases and then goes back to the normal tempo beyond that distance. I'll add the elements you suggest. Would i place that between the calling the array and the loop node?

          I forgot to mention in my original post that i am doing this in the level blueprint - i know not the best idea but coding is not my specialism so have made some errors.

          Comment


            #6
            Had another go at it and have got a little closer to what i want to achieve. The problem now is the less than float setting, set to 400 and if another AI is over 400 the tempo remains faster.

            I'm still not selecting the closest AI but any AI that is under 400 distance - at least i think that's what is going on?

            any ideas?
            Attached Files
            Last edited by LCCTB; 03-11-2018, 04:25 PM.

            Comment


              #7
              You should not use GetAllActorsOfClass during gameplay as it is a very slow function. Keep an array updated with the current NPCs and use that instead. GetSquaredDistanceTo is also faster than GetDistanceTo.
              Click image for larger version  Name:	GetDistanceToNearestNPC.PNG Views:	1 Size:	149.5 KB ID:	1442068
              Last edited by GarnerP57; 03-12-2018, 08:48 AM.

              Comment


                #8
                Originally posted by GarnerP57 View Post
                You should not use GetAllActorsOfClass during gameplay as it is a very slow function. Keep an array updated with the current NPCs and use that instead. GetSquaredDistanceTo is also faster than GetDistanceTo.
                Click image for larger version Name:	GetDistanceToNearestNPC.PNG Views:	1 Size:	149.5 KB ID:	1442068
                thank you. I am away till Friday so I will try this when I return.

                Comment


                  #9
                  Originally posted by GarnerP57 View Post
                  You should not use GetAllActorsOfClass during gameplay as it is a very slow function. Keep an array updated with the current NPCs and use that instead. GetSquaredDistanceTo is also faster than GetDistanceTo.
                  Click image for larger version Name:	GetDistanceToNearestNPC.PNG Views:	1 Size:	149.5 KB ID:	1442068
                  Perfect, that's fixed it. Followed what you said and its worked perfectly for me. Thank you so much for your time.

                  My last problem is when all the NPC's have been killed the value returns 0.0 at which point io want the tempo to be at the slow (normal) tempo so i need to figure out how to do that.

                  Thanks again GarnerP57

                  Comment

                  Working...
                  X