Find a New Direction

Jared Amlin
3 min readDec 10, 2020

I mean this quite literally, only I am speaking to my new enemy type that will act as a battering ram when it gets close enough to the player. I find myself coming back to something I learned when making the heat seeking missile. Position A minus Position B = new Direction. A missile will use it’s own position (A), minus the Position of the Target (B), to get a following distance and direction. It was explained in a Brackey’s video as Direction A + Direction B = Direction C. I used these principals to not only get this new enemy to chase down the player and try to ram it, but also to ignore it if a designated distance is not met. I found a reference online for a dodgeable heat seeking missile, but it was made for 3D applications, and was making my enemy move off the Z-axis when I tried it. One thing I took from that lesson was something they called focusDistance. It was representing the space between the missile and the target, so you can tell the missile to seek or not, depending if it is inside or outside of that focus distance. I made a variable called targetingDistance, and let it run the same kind of function. Getting my enemy to chase the player constantly was not too difficult. I used the same scripting commands as my heat seeking missile, only I changed the target to look for the Tag “Player” rather than “Enemy”, as well as changing the movement direction and speed. The chase was working great, so now I needed to only call it if the player is within that targetingDistance. While at dinner, I came up with a possible solution (my poor family could tell my mind was very distracted with coding). I wanted to break up my enemy movement with an “if” statement. If (location of my enemy minus the location of my target is less than the targeting distance, seek and destroy. Else, continue normally.

I was having trouble entering the statement as I envisioned it with my head being that Visual Studio didn’t like my reference to “position” when referencing the target in my “if” statement. I did some research that led me to a function called Vector2.Distance, which returns the value of the distance between A and B. This is exactly what I needed. Here is the final script that got me pretty much where I needed to go. The command as far as what the enemy does when it locks on to the ship and then the ship gets out of range could use some finesse, but it is generally working. I have included some notes where I want to address the movement when the player slips the targeting.

New enemy type that chases the player when close enough.

Other than that, I also need an enemy that can use shields. I decided to make another enemy preFab, yellow this time, and make this ship use shields and ram the player. Why not try to check both of those boxes with one enemy? I had a run in with my shields where when active, the enemy is completely indestructible. Not great when that same enemy is targeting the player and ramming it to death. My next goal here is obviously to get those shields working on the enemy to take one hit before disappearing and making the enemy vulnerable. Being that a shielded enemy that seeks the player can be deadly, I though it would be best to not give this enemy any kind of fire power.

I have an idea using the same distance calculation and homing action that I want to try and use to check off another box….the player being able to draw powerups toward them with the push of a button. Hopefully by the end of the day Thursday I will be able to report back if that idea worked or not.

I am always thankful for the help, advice and opportunity I have been given concerning this wild journey though game creation in Unity, and I am excited to try and implement these next functions.

--

--

Jared Amlin

I am an artist and musician, that is currently diving headfirst into game development with C# and Unity3D.