Unreal Engine Utility Functions #1: Random Point in Sphere

It has been quite a while since I've posted something here. More than a year actually. The main culprit, being that creating gameplay systems with blueprints is a far more enjoyable process than writing about it. But I've decided to write "again", by making some free time every week, even if it's just for a few hours. So given the already established lack of motivation to do so, it made sense to start with something small. Hopefully that will make it easier to just keep going without giving up on writing for another year. And without wasting any more time explaining my motivations (or lack thereof), let's get right down to the actual project.

So I was going through a backlog of ideas that I've always wanted to try out, but with the hope of finding something simple, yet enjoyable that I can do after work. And while this particular choice is not as exciting as say, creating a gameplay mechanic itself, I figured having a blueprint library for Utility Functions would be a good idea. The functions can be implemented one at a time quickly and might also prove useful in future projects. As good a place to start as any.

And as the name suggests, this first function will give us the location of a random point inside a sphere, given its radius and the center location.

So if you're new to blueprints and reading this, what's happening here is that we first take a unit vector in a random direction and multiply it with a value less than the radius of the sphere.

This will give us a vector whose direction is randomized, but with a magnitude (or length) less than the radius of the sphere.

Now imagine adding this vector to the center location of the sphere. Since its magnitude is lesser than the sphere radius, the final location after addition will always be inside the sphere.

And the randomized direction of the added vector means we get a completely randomized point within the spherical volume each time.

Here is the final result of the function being used in a test scenario:

And that's all there is to it. I hope to continue adding more functions to the library over time. If the library ends up getting a decent collection of useful functions (before the motivation bar runs out), maybe I'll share it on GitHub/Gumroad. But that's for later. One step at a time for now.

Unreal Engine Utility Functions #1: Random Point in Sphere