Using a cosine wave in Unity for enemy projectile’s movement
The exercise here demonstrates implementing a wavy movement of a projectile being fired from an enemy in a 2D game.
Before moving ahead, please make sure there is a separate enemy prefab and an associated projectile prefab in place.
The idea is to move the blue ufo on the X-axes and keep it bringing down before it reaches a certain Y value when it should exit the screen. So, the player and the ufo would not be touching each other.
The projectile shall originate from within the Ufo and then, move down in a wavy movement just like the ufo. The projectile should damage the player’s life if there is a collision. And lastly, if it goes out of the game view, basically it should be destroyed.
Let us begin.
- Create a script for the ufo prefab’s movement and assign it to the prefab.
- There are two math values that have a role to play here.
Frequency: The speed at which the projectile will oscillate between two extremes.
Amplitude: The total width of the wave.
- Declare these two variables in the script.
float frequency = 1.5f;
int amplitude = 9;
- Initiate the prefab at a preferred location in the start method.
transform.position = new Vector3(Random.Range(10, -10), 7.0f, 0);
- In the update method, create another variable that is going to get us the value of transform.position.x.
- Use Mathf.Cos( ) function and pass an argument of “Time.time * Frequency” in it and, multiply the result with the amplitude.
This is taking the real-time and multiplying it with a chosen frequency to make the oscillation smoother and later, multiplying it with the amplitude to increase the width of the wave.
- Save this script and create another one for the laser prefab that this ufo shall fire.
- Declare two variables for the frequency and amplitude of the laser prefab.
private float frequency = 10.0f;
private float amplitude = 1.5f;
- In the start method, we need to get the x position on which the prefab gets instantiated. So, declare a variable to hold of x value here.
- In the update method, everything remains the same as the ufo except for one change without which, the laser prefab will keep getting to the centre of the screen immediately after the instantiation.
- We are adding the base value we had captured in the start method to the newly create value using the Mathf.cos ( ) function.
- Lastly, we need to instantiate the projectile from the ufo script so that it starts right where the ufo would be at a given time. So, go back to the ufo script and add a fireLaser( ) method to make this work.
- The method contains a cooling mechanism to only fire at a regulated interval.
These configurations along with the other basic implementations that may be needed such as calling the player’s damage method, scoring method, or updating the spawn manager if the player dies shall be done as well.
Also, I have implemented the exit method by checking the x and y position at a time.
After which, the result should resemble something like this.
Thank you very much