Simple raycast from camera

I have a blueprint question for the 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 the length of the 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 the PC
  3. Get Control Rotation of the 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 the 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:

Rama

Hey venain,

Rama 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 Documentation

Hope that helps!

Ben Halliday

Hey thanks for the great info Rama and Ben!

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

Thanks again!

I was able to do this using the camera world location as the start point and everything else as rama said, I’m just curious though, why exactly add 12000 to the starting location? I tried playing with this number and it just seems that the 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 the world (120m). If you use a shorter trace, it should work, but it just might not be far enough to hit the thing you want.

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

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

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

#Mouse Loc and Dir

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

to do the trace, the end point should be

Mouse Location + Mouse Dir * 24000

or whatever trace length you want :slight_smile:

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

I’m currently just storing the hit object location and comparing it with my player controllers location to do a distance check, so it’s not the 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, the problem was just the order of operations. Just make sure you multiply the forward vector of your start point by whatever length you want it to be, then add that result to the world location of your start point. I attached a graph that points this out, thanks for the help everyone!