# How Do You Raycast At A Random Location In Area

Hello

I know how to raycast from the center of the camera, but how would you raycast in a random place on the screen in an area.

To explain further I am making a shooter game and I want for there to be a blueprint for if you do not zoom in and use the scope on your gun, you will shoot in a random place on the screen but in a confined space.

like in this picture http://i.ytimg.com/vi/S0780E8mBpQ/maxresdefault.jpg

Unless you zoom in you will shoot anywhere in the white diamond . How would I make that using blueprint?
Also would i make the white diamond in blueprint or in Umg? and how ?

Thanks,

Mike

The cross-hair has to be done in UMG or at least the HUD.

Basically you have your trace from the center of the camera straight forward. I’m not sure how exactly you do it but one can just get the forwrad vector and multiply it with some distance. Now to have it not exact but randomized you have to rotate that vector a bit from it’s default angle to a random one between your points.

First I would get a random float between -1 and 1, then multiply it with the maximum angle it should spray away from the default location and rotate your raycast by that value. Since this maximum spray number can easily be a variable you can increase it the longer one shoots. You can also provide it to your UMG to set the size of your diamond there.

Now getting the right numbers here will take quite a bit of try and error. Also properly rotating the raycast is a bit of vector math but it should be reasonable to get working.

I hope this helps.

Cheers.

I still do not fully understand how to rotate the raycast and make it spray anywhere, could you possibly show me how to do it via blueprint?

Thanks,
Mike

Well generally that’s vector math but Unreal even provides a function called “Rotate vector”. You just have to input your angle and it rotates it accordingly.

There really isn’t much to it.