How to target a specific actor?

I am making a tree cutting system. There is an invisible rectangular collision box attached to the head of the third person character. When the collision box touches a tree and you press F, that tree will be destroyed. However, I have a problem where more than 1 tree is getting destroyed at a time when more than one tree is getting touched by the rectangle. So is there a way to destroy a specific instance of this actor since they are all apart of one actor called “tree”.

The target tree is likely the one the camera is facing, or the tree that is the closest to the player when the action button is pressed. You can perform a trace emitting from the player, toward the player forward vector (multiplied by some range-determining amount) and return the first tree object hit. Only call the destroy on this actor, rather than the resulting actor(s) from the overlap.

If you want to do it range-based you can determine the closest of the overlapping trees by doing
(Get overlapping actors) (note: you should have your collision box only triggering for relevant items, use a collision group)

foreach (collided trees array)
Player location - Tree location → Vector length

If the length is lower than the previous length, save the index.

When loop is finished, you can use this index to find the specific actor from your overlapped tree array, the tree that should be destroyed.

I am very new to unreal engine and brand new to blueprint scripting. How would this look?

below are some examples of the type of thing that Unfathomable mentioned.

The first picture shows how you could use a line trace to locate an object. in this example we draw a line from the character to a location 500 units in front of the character. then the first actor this line hits is returned as the out hit pin. we then compare this result to the class that we are looking for via the equal (==) node. so if the hit actors class is the same as the the class we are comparing to, then the branch is true and we destroy the actor. now there is variation to this method where you use a line trace for objects or you could use a shape trace like a box trace which is more similar to your current setup.

the second picture shows the proximity method. i made it as a function here but its not necessary. the way this one works is that it gets all actors of the tree class that are overlapping the character then compares each to see which is closer. let me try to explain this better. lets say your character is overlapping 5 trees, you begin by setting the closest tree variable to the first tree on the list (tree 1). you then take the next tree and compare its distance to the character to the distance of tree 1 to the player. if tree 2 is closer then it gets set as the closest tree, if its not closer then we move on to tree 3. you do this for all trees and when your done your variable is set to the closest tree. ok now as an example of the above lets say we have 3 trees with the following distance to the player, tree 1: 3Meter, tree 2: 1meter, and tree 3: 5meter. so we set tree one as the closest since its the first, then we compare tree 2, so we say is 1 meter less than 3? yes so we set tree 2 as the closest. then we move on to tree 3 and compare again, is 5 meter less than 1? no so we dont set the variable.

hope that helps a bit.

I think the issue I am having is that the destroy and overlap test is in the blueprints for the tree, not the blueprints for the actual character. So when i go to set that subclass from the array element it wont let me. (it says it’s not compatible) What do you think I should do here?

if your overlap and wood chopping script is in the tree then thats even easier. eliminate the extra collision on the characters head then just add a small collision to the tree. from there i believe you just have a input issue. to solve this you just need some script to enable and disable input for the tree bp. below you will see an example of how to do this. just substitute the cast to the class of your player, or instead of the cast use a get player character → equal == → branch combo and connect the other pin of the equal to the other actor pin.

I’m not sure why I’m having problems with this; so I have attached a screenshot of my blueprints. Could you tell me what I’m doing wrong with this? alt text

you are over-complicating all of it. if your character is the third person character then you can set it up exactly as i showed. your method of casting is a bit redundant, if your trees are close enough that that you need to specify a specific component then overlaps are probably not the way to go. the setions where your setting the tree variable is also not needed since if input isnt enabled then the input event wont fire, this means your branch and gate become redundant. i dont know what else is before the branch so i cant comment on that. overall though your script is overly complex and redundant. you want to aim to make your scripts as simple as possible so they are better performant, easier to understand, and easily debugable.

Thank you for the tips on the complication of the script! I’ll definitely check up on that. You said that overlaps are probably not the way to go. I have tried line tracing before and it just hasn’t work out very well for me. Is there a way I could fix this with the system I have?

(This is just supposed to be a very small project so I don’t wanna redo everything I’ve already done. I’d rather just get this one over with and move onto the next project. That project will be where I spend more time on the layout/complexity of the scripting.)

what i said was that if your trees are so close together that your overlapping many of them then overlaps may not be the way to go. i dont know about the placement in your level but your trees are probably spaced far enough apart that if you get rid of the extra volume you added and just use the capsule for overlaps it will probably work fine. if you were making a fps game then id go with a line trace for its precision and so you could use a crosshair. in your case though overlaps should work fine. if you go with a script like i posted earlier and you limit the overlap components to a volume you can use accurately then what you have now should work with no problem.

if your still having trouble post a picture of your character (to see collision volumes) and how the trees are setup in the level see we can see spacing compared to the character.

I have placed trees deliberately close together in one particular area to create a type of obstacle, so to remove them and progress you have to destroy them one by one and a tree is added to an inventory. Like I said, line tracing hasn’t worked very well for me and I’m just trying to get this project finished as quick as possible. So do you know if there is something I could add to the already existing blueprint for the collision rather than redoing it with a line trace. I just want to be able to destroy one actor instead of destroying all the actors that are overlapping with the cylinder.

Below is a screenshot of the interaction collision attached to the third person character and a screenshot of the tree placement in the level.

given your situation you really need to modify your approach id say. you need to either: be calling an event from the characters perspective so you can use some decision making script to say which is closest or use a line trace, or eliminate the horn and the excess overlap volumes and modify your scene to make it work as is. when your character can overlap all the trees at once there really isnt much choice you need to have a way to determine which tree to target, this means either using a more selective method or having a way to sort through all the overlaps.

you mentioned not wanting to change this project so you could move on to the next, honestly im not a fan of that. give this project your full attention and take the time to do it right before even thinking about the next thing on down the line. if your not concerned with this project why bother?

So do you know how I could make a way to sort through multiple overlaps? Or how to make it so when the cylinder is touching more than one tree, destroying will only target one of these overlaps, or a random one?

Mentioning about how I am going to move onto the next project:
I have said that I am very new to unreal. This is the very first level I have made in UE4 and I’m just trying to understand some of the basic mechanics of unreal such as blueprints and landscaping. This project wasn’t planned to be a long term thing, just something to help me get familiar with UE4 before I can actually create something I can be proud of. Right now I’m focusing on simple, easy and I’m weighing learning over the quality of the level.

the way you have it setup now you cant sort them or select one at random. you would basically be trying to call a script to find the closest for instance from all the overlapping trees. i mean i guess maybe it would work if you disable consume input on your input event, got a reference to the player, found the closest tree to the player, compares the result to self, delay and then call destroy actor if they are the same, and you do this on every tree. its jsut not a great solution and could result in bugs.

as for the other point i wasnt saying go for perfection in quality, i meant do things the right way and in a way that makes sense. most importantly focus on the thing your working on and not on “lets just get this over with and move on”.

If you could send a screenshot of how that would look I’ll try that out; even if it’s not the best way to do this.

Like I said, I am focusing on how much I can learn about UE4 before I go for anything else and from the beginning this project was meant to be a smaller level just so I could have a sort of backdrop while I learn about unreal.