Game Dev in Progress… Day 13

Cristian Aspacio
3 min readNov 7, 2020

--

Photo by JESHOOTS.COM on Unsplash

Good evening and Aloha Friday! Welcome to another entry of Game Dev in Progress. Today was a tough day, I only got through one of the four features that I wanted to implement today. It wasn’t that I was stuck but more so me taking it a bit slowly and trying to figure out ways to improve my code. The feature that I was able to complete today was Smart Enemy. This feature is to create an enemy type that knows that the player behind it and will shoot backwards. The other feature I was working on but didn’t manage to finish was the Enemy Pickup. This feature would detect pickups/powerups in front of it and destroy them by firing a laser.

The Smart Enemy feature I had trouble with because of how to detect the player. I was very keen on using previous code and modifying to solve this problem. The previous feature that I worked on before this was the aggressive enemy type. The enemy type would detect if the player was close to it and try to ram it. However I had difficulty trying to reuse and modify that code to solve this particular problem. I think this is because I was viewing my logic the wrong way. I was thinking about the distance between the enemy and player not if the player was behind the enemy. I was so focus on that and forgot about the most important part is that there was difference in the y coordinates. When I figured that out, coding was a very simple if statement that checked if the player had a greater y value then the enemy. Other problem was trying to detect x difference between them. I wanted it to fire only when the enemy’s x correlated with the player’s x. This was hard because of how exact it needed to be so instead I used a range. If the player’s x was in this range, the enemy would fire backwards.

After finishing up with Smart Enemy, I started to try my hand on Enemy Pickups. I was going to try reusing the code I had for the previous feature but again stumble on a problem. The player object instance could be grabbed because it was always in the scene. However, how do I do that for a prefab that gets instantiated at enemy time? So my logic and previous code would not help me here so instead I found a better solution. Unity has a class called Physics2D that has a method called Raycast2D. Essentially it creates an invisible laser that fire from a point given a direction. Any objects that lies in its path that has a collider component will register and it will create an object call Raycast2D. This object holds information about the object that was hit. This includes its collider, transform, etc. With this, I can detect objects more smoothly then using what I did for Smart Enemy. I might go back and change that to this.

Anyways I will try to implement this next week and see how far I can go with this. I haven’t tried it yet but they is a lot of nifty tools Unity has that can make a complicated problem into a simple one. Thank you for reading and have a great weekend.

_

Cristian Aspacio

--

--

No responses yet