# Could you explain how this GameObject rotating around another GameObject works?

I'm just learning Unity and I follow a tutorial for a simple game which I want to build up from.

I'm already mostly done with the course and at the end a "boss enemy" gets implemented.

Its special ability is, that it has some other GameObject (a fireball) floating around itself.

It is all done in a single line of code in the update method which I don't quite understand yet, so any explanation would be great.

Here is the code:

private void Update()
{
fireball.position = transform.position +
new Vector3(
-Mathf.Cos(Time.time * fireballSpeed) * distance,
Mathf.Sin(Time.time * fireballSpeed) * distance,
0);
}

• fireball is the transform of the object which floats around
• fireballSpeed is a float which indicates the floating speed
• distance is the distance from the boss enemy

I get that we are just updating the transforms position but I don't quite understand the Time.time * fireballSpeed part. I also get that Cos and Sin are returning positions for X and Y but I need to improve my math knowledge to completely wrap my head around it to be honest.

So any explanation would be great.

• Does this image answer your question? en.wikipedia.org/wiki/Sine#/media/File:Circle_cos_sin.gif Oct 19, 2021 at 15:33
• It certainly helps, thanks alot! But i still cant figure out the Time.time * fireballSpeed part. What bothers me is, that the Time.time is always increasing right? How does it work that the object is flying the same circle all the time? Oct 19, 2021 at 15:46
• An interesting property of the sine and cosine functions is that they are repeating indefinitely. So no matter how large the input value gets, the output value keeps oscillating between +1 and -1. Oct 19, 2021 at 15:49
• This looks to me like a math question, rather than a game development question. You might want to refresh your understanding of trigonometry and the unit circle first, since tons of high-quality guides about that topic have already been written. Then you can ask here if you have a game-specific question about how to apply that math to serve your particular gameplay needs. Oct 19, 2021 at 15:53

The purpose of the sine and cosine function is to turn an angle into X and Y coordinates. Here is an animation from Wikipedia (source) which visualizes what these functions do as the input angle is incrasing: Although there is one aspect you might want to keep in mind: Floating point accuracy. Time.time is a float. The larger a floating point value gets, the less accurate does it become. This can be a problem in a situation like this where the least significant parts of the number are the most important. So when your game runs for a very long time, then this rotating animation might get a bit choppy.
One possible fix is to use double accuracy instead, which means that you use Time.timeAsDouble instead of Time.time and System.Math instead of Unity.Mathf:
-Math.Cos(Time.timeAsDouble * fireballSpeed) * distance