Make the player character rotate to look at an actor

What I’m trying to do is make the character aim at an actor when pressing a button, similar to the old school resident evil aim system. I’ve set up a blueprint to do so and the character rotates to aim, but it always face the same fixed point in the level, not the actor. I know the problem is that I am not referencing the target actor properly, but can’t get my head around it. Here’s my BP. http://i.imgur.com/ehQGKjE
sorry for not posting it as an attachment, i’m on an old phone.

Instead of using ‘Set Relative Rotation’ of the Capsule Component just use ‘Set Actor Rotation’ and keep it as Self (since this is on the Character.) Also, don’t use ‘Get Player Pawn’ unless it is actually a Pawn, use ‘Get Player Character’ instead since (I’m assuming because it’s the template) it is a Character rather than a Pawn.

Yes, most likely you aren’t referencing the Target properly. To help you solve that I would need to know exactly how you are (or want it to) “see” the target. There are many methods including perception, line traces with distance calculations, etc… To help with this I need more info. You could do a ‘Get All Actors Of Class’ and set it to find all of your targets when you right-click (your aim function) then loop through the array it passed through, check their distance (Get Distance To node), set it into a ‘Target’ variable (declared as the same type as your target) and on each iteration of the loop, check to see if the new ‘Get Distance To’ is closer than the last one and if so, Set the variable again to the new target, if not keep iterating. Then when it’s done iterating (completed) you run your rotation code. This is probably the easiest but be aware that running ‘Get All Actors Of Class’ can be very CPU intensive if you have a lot of the same Class on the map. This node should never be used in something like a Tick or Tick-like event but in something where you have to manually push a button, like with your aim event, it should be fine but again, as long as their aren’t a ton of the same Class in the world.

Hope this help! If it did, acceptance of the answer would be greatly appreciate!

Thanks,
Jesse

WoW, that was a fast answer! Regarding your question, I want to use a Sphere Trace By Channel to “look” at the target. I tried (and failed) to do what you suggested, but I dont get the whole idea. Here’s what I got.

And yeah, I’m using the Third Person Template with the Animation Starter Pack.

What behavior are you trying to get out of this? Why are you getting all actors every time you press a button? Why not test actors you hit with the sphere trace? 2000 is a huge radius that probably covers everything - why are you doing that? What are you doing with Distance To? Why is your end trace point along the Z axis? What’s the point of Getting All Actors if you’re doing a sphere trace?

I agree with Nick. If you are using a collision sphere (not trace) you can simply use overlaps. When a target overlaps the collision sphere, add them to an array and when you right click to aim you iterate that array to see which is the closest target either by distance and/or angle (you don’t want to snap around backwards if that’s the closest target.) When a target is destroyed or it leaves the collision sphere (End Overlap) you remove it from that array. For Get Distance To ‘Target’ would be your target but Other Actor would be ‘Self’ as the character. Put that value into a local float , and run a check to see if the current value is less than the value in the float. If it is, add that Target to your target variable if not do nothing. At the end you will have you closest target by distance. But you’ll probably want to formulate an angle check in here as well.

You can also run the sphere trace on each click rather than the overlap. Traces are a relatively cheap operation. No need for Get All Actors with the trace. You only get one result with a trace which should be the first target it hits (the closest) but again this won’t care if it’s behind you causing you to snap around which may look unnatural.

Having a little success here. I did what you said about putting the targets into an array ( did this on the level BP on the first screengrab), now the character rotates to aim at the target. The problem now is it always aims at the same target, even after implementing the Get Distance To node. Here’s the BP

Rotation toward a target