0
\$\begingroup\$

I am working on a game in Unity, and I currently have a game object (object A) located at the origin. I have a child of this game object (object B), that is 10 units back on the z-axis. When I rotate object A, it causes object B to orbit at 10 units from the origin.

With that being said, I have many game objects that orbit this same way. I want (at any point in time) to be able to spawn a new game object a certain distance away from object B. This certain distance is really going to be something like 60 degrees around the sphere in any random direction from object B. So when I create a new game object, I will create a new pivot point object (object C) and a new child of that pivot point (object D) at 10 units away on the z-axis.

I know I need to create a new rotation from the current rotation of object A, but this needs to be in a a random direction and the new rotation should be 60 degrees in that direction.

Let me know if that doesn't make sense, or if you need any other information.

I am not really sure how to even go about determining these rotations, but I am currently trying to use Unity's Quaternion class which I believe will work, but I think I need to step back and see if anyone else has a better way to go about this.

Appreciate the help!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

For anyone who is curious, here is what I ended up doing.

I created a new rotation using Quaternion.AngleAxis:

Quaternion.AngleAxis(60f, Random.insideUnitCircle);

I get a random direction (axis) by using Random.insideUnitCircle. Then I pass how many degrees I'd like to rotate in that direction. This returns a new quaternion with the proper rotation.

The last thing I needed to do is combine the new rotation with the current rotation of object A so that the new rotation is "relative" to the rotation of object A. This is done by multiplying the two rotations:

var newRotation = objectA.transform.rotation * Quaternion.AngleAxis(60f, Random.insideUnitCircle);
objectC.transform.rotation = newRotation;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .