Another case of Keep It Simple Stupid. I wanted to create enemy space ships that entered at the top corners of the the screen and flew diagonally across. While searching, I found multiple different ways to do it. The search for ways to do certain things in unity can be tricky. There are different ways to manipulate Vector3, sometimes when searching it is hard to find the right path if you are not asking the right questions.
First I tried to use:
transform.Translate(new Vector3(.5f,0,.5f)* _enemySpeed * Time.deltaTime);
While it did move diagonally from where I wanted, it dropped back to the background on the z-axis. Not what I was looking for.
Then I tried:
transform.Translate(Quaternion.Euler(0, 45, 0) * Vector3.forward * _enemySpeed * Time.deltaTime);
It was another one that dropped it back on the z-axis. It also spawned it in the wrong area, so that didnt work. Back to the documentation then. I thought to myself in the first place that it couldn’t be as easy as adding two static properties of Vector3 together to make a diagonal direction. Of course though, keeping it simple was the best way to go. It seems to work out that way quite often, but only after I tried the difficult stuff first.
This is what I ended up with:
var rightForward = Vector3.right + Vector3.down;
transform.Translate(rightForward * _enemySpeed * Time.deltaTime);
It ended up working exactly how I wanted, I made the diagonal enemy smaller so it was a little harder to hit and because it is moving diagonally it is moving faster. I made one from each sideJust another reminder for me that sometimes the least complicated solution is the best. Here is the entire method with the three different types of enemies: