On my 20th day, my first game started to look and feel like a real game.
Things I’ve done today:
- Enemies now try to ram the player if they are close to the player. If player evades, they keep on moving.(Kamikaze!)
- Player can now pull the powerups to himself by pressing ‘C’ button.
- Updated the enemy spawning with shield, as the wave # increases, the probability of having a shield increases.
- Fixed some bugs, enemy2 is now moving with a script rather than anim — it was a bad idea but an experience is gained :)
- Reached halfway on building the boss. Basic skills and health bar is complete, I need to decide on it’s shooting style — no rush.
I’ve actually got more comfortable with Vector3.MoveTowards method, as both the powerup and enemy uses this method in order to get closer to the player.
Lets first look at the way how MoveTowards defined in Unity:
Basicly, you can realize that it takes the current position and the target position for the start and finishing points. maxDistanceDelta is the amount of distance that the game object will move per call. Meaning that, if we are going to use it within the Update function, movetowards function will be called every frame, causing the relevant game object to move at a distance of maxDistanceDelta per frame. In order to gather a smooth movement, we need to multiply the maxDistanceDelta value with Time.deltaTime.
If you do not multiply the maxDistanceDelta value with Time.deltaTime, you will realize that the game object will reach to the target destination at a glance, which is almost impossible to catch with the eyes.
MoveTowards function returns Vector3 as a value. As we are actually changing the position of the game object, it requires a position value on the left hand side of the equation.
As can be seen above, we are telling the game object to move to the player’s transform position with a speed of 4 on every frame, once the MoveToPlayer method is called.
I really love trial and error in Unity, rather than typing just 4f*Time.deltaTime, put a variable in there with a serializefield attribute and try the speed by yourself.
Lastly, to make things more clear, don’t forget to check Unity documentation on MoveTowards method by clicking here.
In order to make sure that enemy object will continue on moving on its default path, I have declared a distance to player float variable, and used Vector3.Distance method. Vector3.Distance results in a float value, calculating the distance in between two objects.
As you can see above, RamPlayer method is only called if the distance is below 4 units.
Here’s a gameplay video where we can pull the powerups to the player ship and enemies try to ram us if they are close.