How to create a new Vector3 using an angle and a distance away from another Vector3

I am trying to program a sort of AOE in a game - there is a prefab showing the circle the ability can hit, and the projectile should fly to a random point in the available AOE. Since it’s a circle, I want to use a random angle and a random offset from the center:

targetOffsetAngle = (Random.Range(0f, 360f)) * Mathf.Deg2Rad;
targetOffsetDistance = (Random.Range(0f, aoeRadius));

I was hoping this would give me enough information to create a new Vector3 based off the target position, the angle, and the distance from it. The AOE is a flat area. So far I haven’t found a way to do it, and it might go out of my C# knowledge. Any help?

I have realized that I actually needed to do this issue on paper and that my past math teacher probably felt the disturbance in the air as I pulled out the SOH CAH TOA table

        targetOffsetAngle = Random.Range(0f, 360f);
        targetOffsetDistance = (Random.Range(0f, targetScript.aoeAttackRangeSize));
        addX = Mathf.Sin(targetOffsetAngle) * targetOffsetDistance;
        addZ = Mathf.Cos(targetOffsetAngle) * targetOffsetDistance;
        offsetPoint = new Vector3(addX, 0, addZ);

The AOE is flat on the ground, so I use X and Z axis instead of X and Y. The offsetPoint vector is used later in code.

1 Like

Assuming that you’re working in 3D, first you’ll need to create the euler angles for your angle:

Vector3 eulerAngles = new Vector3(0, angle, 0);
(where angle is a value between 0 and 360)

The reason the angle goes in the y axis is because we need to rotate along the y axis to rotate in a horizontal plane.

Then we need to convert the euler angles into a quaternion (quaternion is a data type that is used to do rotation calculations):

Quaternion quaternion = Quaternion.Euler(eulerAngles);

Then we can create a direction vector using the distance value. We can put this in the z axis since the z axis is the forward direction. This way an angle of 0 would be facing forward.

Vector3 direction = new Vector3(0, 0, targetOffsetDistance);

And we can multiply the quaternion with this vector to create an angled direction vector.

Vector3 angledDirection = quaternion * direction ;

Finally we can just add this vector to the original position to get the new target position;

Vector3 targetPosition = transform.positiom + angledDirection;

1 Like

Your method also works well and it’s basically what happens under the hood if you use the quaternion method anyway. Using quaternions may just be a bit more readable typically, although in this case maybe not really.

Random.insideUnitCircle

1 Like

It’s really good to know for the future that quaternions do this, as probably it will be useful down the line with other features not so easily solvable by simple math. Thank you for your insight! I was originally looking for a solution like yours too, as it made more sense to me, but didn’t know of Quaternions.

You’re welcome! Yes for more complex calculations using Sin and Tan may start becoming confusing while quaternions will end up being more handy.

Note that what’s handy is converting between euler angles and quaternions, and rotating vectors using quaternions. The inner workings of quaternions themselves is complex and should be treated like black magic as in just be happy that it works without understanding the why lol.