How do you script an event when the character is looking at something?

Think of the game ‘Amnesia’. If youve never played it, whenever the player looks at something scary or unnerving, your “insanity bar” is lowered. Similarly, to regain your “sanity” you need to look into the light, then the bar becomes filled again. How would this be done in blueprints? Thank you!

to know how much you are looking at a target, you should use dot product.

normalize( target’s location - camera’s location ) Dot ( Camera’s rotation’s forward vector )

that will give you a number between -1 and 1. 1 means the object is centered in your camera’s view, -1 means the object is directly behind the camera, and 0 means the target is orthogonal to the camera. so you can check if this result is greater than .5, and if so, you can trigger an event, because the player is looking near the target.

thanks for your help! I’m a beginner so bear with me. In my character blue print, i cant get the target actor because its in the character class. Am i not supposed to do this in my character blueprint? Basically, Im trying to make it so when the player looks at a certain actor, an event triggers.

you can use get all actors of class, or get all actors with interface, to get a list of actors. if everything you can target implements an interface called iTarget, you can get all actors with interface iTarget.

another option, is to use an overlap event. your pawn/character can have a large sphere around them that overlaps nearby objects. then you can use GetOverlappingActors, and use dot product to find out how much you are looking at each actor.

Ok i like the sphere idea. But, how do i get the actors that i want out of the array?. I just want something to happen when the character is looking at 2 certain actors. How do i pick those 2 out of the array?

to clarify, i dont mean the character has to look at both actors at the same time. I mean there are only 2 actors in the game that will trigger an event when the player looks at them

GetAllActorsOfClass lets you filter by a type of actor, so if both actors are the same type of actor, and the only existing actors of that type, then the list returned from GetAllActorsOfClass will only contain those 2 items.

You can also use a ray trace node to set a bool. I used this for a laser sight program.

sorry for the late reply I wasn’t able to work on this for a while. How do I get the cameras rotation? I have Get Socket Rotation with the first person camera as the target and its not working. What do I need to fix? I will post a screenshot below of my FirstPersonBP. Thank you

Pawn’s have a function called GetControlRotation, which is the rotation of the camera in most cases. ControlRotation is a variable inside player controller.

what happens if there is is something obstructing the players view. Wouldnt this method trigger the event anyway?

you can add line traces to any actors that pass the dot product test, to verify line of sight, but you may want to shoot traces at a few location offsets, or at each corner of the bounding box, instead of shooting at the actors origin.