Day 9 — Phase 1 is Complete!
Awesome day but also frustrating day. I started off very strong in the morning and got my camera shake down and got a good jump on the Homing Missile I wanted to implement and then I got stuck for hours and hours …..and what felt like more hours. I know the standard advice is to step away and come back and let the mind expand and again so the creative juices can flow again. When you hyper focus on something you lose perspective of other ways to tackle the problem and get stuck in a mind loop. Knowing this I still couldn’t let go of my obsession with solving the problems I was having because I was so determined to finish my Phase 1 today….not tomorrow!
With that said however, I got hungry and stopped to eat dinner. After a glass of wine, my creative juices starting flowing again and I came up with a new approach to my problem and voila I got closer to my goal and was able to figure out the rest with some help from GOogle! I do plan on trying to take a more balanced approach next time. I don’t think I am using my time efficiently a lot of times when I get stuck and this did teach me a valuable lesson going forward.
With regards to specifics of what I did today. The camera shake was pretty straight forward. Made a script for the Main Camera. Created a public method called shake that was called from the player side when he takes a hit. This enables the shake and defines the shake duration. Then Void Update handles the shake by randomizing the transform location using Random.insideUnitCircle. At first I struggled because my camera would then get thrown off and my background would also get messed up during the shake ( can see black borders). I fixed it by adding an initial camera position vector3 in Void Start. for the background I was trying to find a way to lock my background by making it invisible to the Main Camera and make it visible with another camera but in the end I couldn’t quite get it to look right and I ended up just stretching the background out a little bit around the borders to encompass the area of the shake so you don’t see black borders during the shake.
I was very excited with the Homing missile system. Creating the Prefab, the Missile script and tying it to the player firing system and Spawn Manager + powerup script was easy enough. I even got super stoked when I found a cool Missile image and a great sound effect to play. Check out the video below to see and hear it in action!
I got very stuck on the tracking system and then later trying to make my missile turn properly. I one point I got everything working but had a whole bunch of NULL errors. In the end I figured it all out and the results were awesome. I ended up using GameObject.FindWithTag to track my prey. I had an issue at first because when the enemy would get destroyed the missiles I fired right after would keep trying to track the same target. This was because I have a 2.5 second delay on the destruction of the enemy to account for his animation. Eventually I figured out I could use the transform to change the enemy tag when he dies. So I created a Tag called Dead Enemy and then changed the Tag in the Enemy script upon Enemy Death.
I used movetowards at first to get the missile to track the enemy and while it worked the image wasn’t turning towards the enemy. This is where I learned about Rigidbody mechanics and the benefits of working with the physics engine. I found a blog from a gentleman that gave a very good explanation on how to use Unity’s rigidbodys physics system for a homing system.
Basically the first step is to figure out the missiles direction and the enemies direction, then using Pythagoras Theorem substract your missile direction from the enemies direction to give you the direction you need to take to intercept.
Next he had to take a step called Normalizing the vectors, which essentially converts the vector to a unit vector. This step I still don’t have a handle on and will look into the “why” later to make sure I fully understand it.
Next you calculate the amount of rotation needed to move towards the New direction and then you figure out the angular velocity by multiplying your angle changing speed Variable * the rotation amount. We also then add forward velocity using Transform.up * our movement speed and the rest is history. You just need to balance your Missile speed with the Angular rotation speed so you get the right behavior for the missile when you calculate their velocities. Since my enemies only move at 4 speed currently. I chose 7 speed for the missiles with angular rotation speed of 210 and it feels pretty fluid.
I have to say seeing the little missiles in action is really awesome and I could not have done it without the information I found on the internet. I am grateful to all the shared knowledge we have access to, this project is really challenging me to think in Mathematical terms again. Fortunately I do have a math degree so it does certainly help but I am rusty…..its been a looooong time since school for me . That being said I know that I will need to dig in to a lot more math problems to be successful in this career. Overall a great day and I plan to continue working on Phase 2 tomorrow. So far I have the ammo visualizer already done.