DevLog 35: Boss implementation — basic movement with Cosine/Sine.

PieceofPhie
3 min readNov 3, 2022

I now have the basic gameplay loop all done and now its time to add a boss at the end of the stage. I came up with the player fighting a lobster that has fused itself with a asteroid or ,’Rock Lobster’. I wanted to have a reason as to why the enemy ships are flying the opposite direction of the player and why they’re asteroids flying towards the player.

It turns out a greedy mining company mistook this lobster as a mineral rich asteroid and angered it.

Here's its basic idle animation. The boss will move left and right to make it slightly harder for the player to hit its weak spot (its tiny claw). The big claw is impenetrable and will block all sources of damage.

Left and Right Movement

As stated previously, This boss will sway left and right. To do this, we can easily utilize Sine and Cosine.

For short, Sine and Cosine is a function that ping pongs between two numbers overtime. By default, Sine and Cosine have a minimum value of -1 and maximum value of 1. So as time passes, these functions will travel between -1 and 1.

The difference between Sine and Cosine is that Cosine is 90 degrees ahead. Without gong to deep into trig, A cosine will travel faster than a sine. They both yield the same results if used independently, however behave differently in other applications.

Boss Script

For now, its a pretty plain script for the boss. I have a function called Movement() that handles …movement that is called within update.

two variables, one to adjust the bosses position and another to increase the amplitude(or how wide the boss can sway back and forth).

I have the bosses Y position locked at 27. Its X position will be equal to Mathf.Cos times the negative amplitude.

Mathf.Cos is the function for cosine. So having the bosses position equal to it every frame will take in all of its values as it travels from one end to the other.

MathfCos takes one float parameter which represents our time. So we simply plug in Time.time to ping pong bettween vaules in seconds.

Multiplying it by the _amplitute (typo) increase the range of which the cosine will travel to and from. I have _amplitute set to 2 so that means the cosine will go from 2 and -2.

lastly, we then send it to the game objects transform.position so that the position of the game object can be affected by it.

--

--