Simple raycast from camera

I have a blueprint question for experts,

I’m trying to recreate a very simple game I made in unity that involved picking up objects and stacking/placing them on top of each other to reach obstacles. I currently have it so that I can pickup objects (this is all in first person), however I feel I’m doing it in an extremely hacky way. I’m using a long thin box component attached to my first person camera and using that as a pseudo raycast.

My question is… Using blueprint, how do I do a simple raycast that shoots from my camera to wherever its looking (and designate length of ray) so that I can get results, designate valid targets, etc?

Thanks!

#Trace

In UE4 they are called traces!

You will want to

  1. Get Player Controller
  2. Get Focal Location from PC
  3. Get Control Rotation of PC

then you want to do a Trace as follows

start = PC Location
End = PC Location + 12000 * ControlRotation As a Vector

You need to convert control Rotation into Vector using appropriate BP nodes

#Easier Way

There might be an easier way, but definitely look up informaton on

#Traces

:slight_smile:

Hey venain,

is correct, Trace Responses are how UE4 handles ray casting. There’s some information on Collision Responses you might find useful here: Collision in Unreal Engine | Unreal Engine 5.2 Documentation

Hope that helps!

Hey thanks for great info and !

However, how do I get Focal Location from PC? only “Focal” nodes I could find had to do with AI controllers…

Thanks again!

I was able to do this using camera world location as start point and everything else as said, I’m just curious though, why exactly add 12000 to starting location? I tried playing with this number and it just seems that ray isn’t shooting out straight at lower numbers, which means using this method I can’t have a really short ray distance, is that normal?

12000 is just a long trace out into world (120m). If you use a shorter trace, it should work, but it just might not be far enough to hit thing you want.

unfortunately, if I add anything other than 10000-12000, trace is no longer shooting straight out, but rather angled down towards ground.

That sounds strange, could you post a screenshot of your logic?

Sure, here’s trace part of my bp, it’s just being activated by On-Tick event.

#Mouse Loc and Dir

This is what you are probably going to want to use ultimately anyway

to do trace, end point should be

Mouse Location + Mouse Dir * 24000

or whatever trace length you want :slight_smile:

I tried this setup and result is same, I have to add/multiply by an extremely high number or else trace doesn’t shoot straight out of camera, but a downward angle.

I’m currently just storing hit object location and comparing it with my player controllers location to do a distance check, so it’s not biggest deal… But I would like to know how to do a small length trace for future uses, but I’m stumped at this point:\

Please post your whole BP graph :slight_smile:

I was able to fix this, problem was just order of operations. Just make sure you multiply forward vector of your start point by whatever length you want it to be, then add that result to world location of your start point. I attached a graph that points this out, thanks for help everyone!