Objective: create a new side-to-side enemy movement.
In the enemy script, we will need some variables:
In Unity’s Awate Function which is called when the script instance is being loaded.Set Vector3 pos to equal the enemy’s transform.position and set Vector3 axis to transform.right:
Now we need our enemy to move down the screen:
Using Mathf.Sin we can set the enemy’s position to constantly move back and forth at whatever value the frequency and magnitude are set to:
The last thing we need to do is have the enemy appear at the top of the screen when it leaves through the bottom: