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, 
  • 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.

  • 1
    \$\begingroup\$ Does this image answer your question? en.wikipedia.org/wiki/Sine#/media/File:Circle_cos_sin.gif \$\endgroup\$
    – Philipp
    Oct 19, 2021 at 15:33
  • \$\begingroup\$ 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? \$\endgroup\$
    – pawx95
    Oct 19, 2021 at 15:46
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Philipp
    Oct 19, 2021 at 15:49
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Oct 19, 2021 at 15:53

1 Answer 1


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: sine and cosine animation

An interesting property of these functions is that they are repeating every 360°. The sine of 10° is 0.17364. The sine of 3600010° (1000 full rotations and then an additional 10°) is also 0.17364. No matter how large the input value gets, the output value keeps oscillating between +1 and -1.

Which means that feeding a continuously increasing timer into the sine and cosine functions is a perfectly valid method to create a continuous rotation.

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 

You must log in to answer this question.

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