I will check the angle θ between the direction (vector) of camera, & direction (vector) of camera to object center.
If θ is within a certain range (θ <= θ_MaxRange), base on size of object, then object can be interacted.
What do you guys think about this solution?
I am pretty new to blue print (got UE4 only 1 week+), so I need to figure out which fuction/node will call out the vector functions (like angle between 2 vectors) that can be used & all (would be nice if someone can give a hints), but I think it will work, …or not.
I was going to say just use a cone trace, but I just looked and they don’t exist. There are capsules, boxes, spheres, and of course lines, but no cone. That’s really surprising to me, I could have sworn one existed.
So. Create a function lib and just copy those functions over after that you can use the GetYawToVector to compute the Yaw to the given vector. Wich means u plug the inputs as follows. Vector is the Location of your Actor you want to check if we look at it. Transform is your character Transform or the Transform of the camera wich you prefer. You then get a Yaw from -179,999 til 179,999 you can then do an InRange check from -15 to +15 and a VectorLenght check if we want to limit the range
well as for the direct look at you could also do a SingleLineTrace my solution is like a Cone Check
Thanks, the 3rd picture is a bit too small to see the details, but the rest are ok (with zoom).
I have like 5 days of Blueprint experience (I do know a bit of kismet, & unreal script) (rest of the week was learning the basics, like materials, UI, lightings), so there is a lot to take in, but I think I should be able to figure it out.
@starseeker If I’m understanding it correctly, I also had to do something like this and this is how I did it:
There’s probably better ways than this obviously, but it seems to work for me.
I added a billboard to my player character, offset by the “range” distance I wanted, and attached it to the camera.
I then set up the blueprint like this, tracing from the camera location, to the location of the billboard. So that only things being both looked at by the player and within range generate a hit result, when I look at them and click the left mouse button.
You may also want to ditch the trigger volume for the object, if all it is doing is checking for the player to be close to the object, and replace it with a simple radius check in Blueprints.
I have my logic set up so that I first check that a target is within the radius of the looking character (AI or player), then within a cone, then finally perform a visibility line trace to it for maximum efficient. But then I am doing this for AI Line of Sight checks as well.
If you want to get the angle between them. Take the actor location that you want to check - player’s camera location. Normalize that vector, then get the dot product of than and the camera’s forward vector. This will give you a value between -1 and 1. -1 is directly behind 1 is directly in in front, and anything between that is the angle.
Edit: I should mention this only compares the locations, this isn’t a visibliity line trace.
I decided to tackle this as a nice blueprint experiment. I ported over a part of the AI sensing component to BP for narrowing the number of actors to trace per sensor tick.
SightRadius is a radius from the player in unreal units.
Compare the magnitude of SelfToOther to SightRadius to make sure we only sense nearby actors
I get the forward vector of the sensor from the camera of the spectator pawn(this image is old and uses controller rotation). Use whatever method for getting a normalized view direction.
Get the dot product between SelfToOther and the sensor’s forward direction.
Compare cosine of view angle to the dot product to get only actors in front of the sensor.
Trace function… I just check to see if most of face of the actor is visible one axis at a time.
Select actors to test and then use CouldSeeActor to limit the total to desired range.
Run traces, breaking early if there is a hit then do actions… like drawing boxes.
TraceHitLoc is not used. I was drawing lines from the camera to the hit; boxes are better.
Yellow out of range.
Red blocked
Aqua/Green is in line of sight.
Thanks, I had the maths figured out. To be more precise. Cos^-1(value) is the angle.
Anyway, I found another method to detect if I looked at an object, which is the the OnBeginCursorOver event. (You have to enable 'Enable mouse over event under Player Controller settings).
On a side note: You cannot edit the properties of the base PlayerController, you have to create a child of it, then set it as the playerController. (took me a while to figure this out!)
However, the OnBeginCursorOver seems a bit inconsistent, & you have not much controls over the area of influence.
I guess in the end, the solution to get the angle between them, maybe better solution, but I have t test it out.
If you just want a single point in front of you, you can use single line trace, start at the camera location. If you want to get the center of the screen just use the forward vector and multiply that by a reasonably large number for the end point.
If you only want to check certain actors, there is a get all overlapping actors node.