r/UnrealEngine5 • • 13d ago

ghost 'captured' by camera mechanic code help

im making a game based on taking pictures and i am trying to create a sort of weeping angel kind of mechanic.
i want it to be that the ghost is just roaming around (which i have) and when the player is looking through their camera and they detect and take a picture of the ghost, the ghost then approaches you, and if you dont capture it it just goes back to roaming but i am not entirely sure how to approach it since i'm not too good with coding and all the tutorials i've found are just for regular weeping angel things and i don't know how to manipulate it into my own thing. any and all help would be appreciated!! i hope this makes sense lol

1 Upvotes

11 comments sorted by

1

u/pattyfritters 13d ago

Make a LineTrace from your camera outward following the camera or player's Foward Vector. When camera button is clicked, check that LineTrace hit ghost (using a Blueprint Interface). If ghost is hit, have it move towards player.

If you need me to break it down further i probably can.

2

u/fishyland 9d ago

Thank you so much!!!! This worked perfectly :p

1

u/fishyland 9d ago

Oh I do have a question, what do I use to check if the linetrace hit the ghost? Would it be a boolean?

1

u/pattyfritters 9d ago

So this is where Blueprint Interfaces come in. You have to Implement the Interface on the Ghost. When your line trace hits the ghost, you check if the ghost implements the Interface then you can use the Interface Event on your Ghost to trigger whatever.

Ive given you a bunch of keywords but Interfaces can be very confusing at first so let me know and i can go into more detail if needed.

1

u/fishyland 9d ago

Yes please! I've only used them a few times so I'm not entirely comfortable with them and I don't really know what exactly to set up in the interface

1

u/pattyfritters 9d ago edited 8d ago

I think this should work and I'm typing all of this from memory sooo.... i hope i got it all.

Right click in your Content Browser and choose Blueprint > Blueprint Interface. Name it BPI_Interact or something.

Open BPI_Interact and name the function OnInteract or something. Save and close this window.

Open your Ghost Blueprint. At the top, click Class Settings. Look for Implemented Interfaces at the bottom of the Details panel. Click Add and find BPI_Interact.

Now your ghost implements the BPI_Interact interface.

Using your Line/Sphere Trace, wherever that may be, drag off of Out Hit and search for the Break Hit Result node. Drag from Hit Actor and search for Does Implement Interface. Select your BPI_Interface for Interface. Create a Branch node and use the Return Value from Does Object Implement Interface.

Drag from Hit Actor again and search for your interface function we named OnInteract. Plug your Branch's True pin to the Input of OnInteract call.

Open your Ghost Blueprint again. On the left you will see Interfaces. Open that and right click on the function OnInteract. Select Implement Event. From this new Event node, add a print string node to test.

Now you have an Event on your Ghost, that is triggered by the player's Line/Sphere Trace. You can use it to do all sorts of things and even pass information back and forth using the Inputs and Outputs of the interfaces function OnInteract.

2

u/fishyland 8d ago

This works perfectly!! Thank you so much for all your help I really appreciate it :D

2

u/pattyfritters 8d ago

Absolutely! Glad it worked!

1

u/Adn0nnymous 13d ago

i wouldn’t use a line trace for this. that would mean you’d have to be looking basically right at the ghost, just having it on your screen wouldn’t guarantee it works. i’d do a wide sphere trace instead.

1

u/pattyfritters 13d ago

Fair enough

1

u/pattyfritters 9d ago

Okay it'll have to be a bit later.