Another day with more time spent wrestling with Unity rather than writing much code .. there doesn't seem to be a great deal of consistency in the commands to do things with the engine, and I seem to spend a lot of time googling how to do something simple (like turn off and on a particle effect, or get access to it). Of course I'm sure this gets better with familiarity of the quirks. I'm gradually trying to get used to monodevelop but I can't figure out how to fix its atrocious text navigation with ctrl-arrow left and right. I've also worked out how to put things in separate C# files, to get things a bit easier to navigate, it isn't like #include in c++.
I had started getting enemies running between waypoints last night. So today I added:
- Enemies turning to face the direction they are running
- Firing bullets (or projectiles) from guns
- Aiming system for the towers
- Healthbars for the enemies
- Enemy alive / dead states and respawn.
- Blood particle system for hits
The aiming system is quite fun. As the bullets travel slowly, if the towers fire when aiming at a moving enemy, they usually miss. So the aiming system uses some maths to predict ahead of time where the bullet will hit the enemy given its velocity and the velocity of the bullet. This actually works when the enemies are running in a straight line, but all bets are off if they turn a corner.
This can work well with different tower types firing bullets of different velocity. Higher velocity bullets are more likely to hit, however lower velocity bullets could do more damage?
At the moment the calculations are 2d but it could be fun having some 3d projectiles launched into the air, maybe with area effects.
The placement of waypoints is manual so far, so I want to write an auto system for this, and make sure that towers / buildings will not be built along the path (unless maybe placed by the player as a block).
(shamelessly ripped from https://gamedev.stackexchange.com/questions/14469/2d-tower-defense-a-bullet-to-an-enemy)
// Aiming with slow bullets has to take account of the fact the enemy is moving.
// So we use some maths to predict where the enemy will be when the bullet hits,
// and the correct angle to aim 'ahead'. This works when the enemy is going in a straight
// line, however when it turns a corner the shot will probably miss.
Vector2 ptT = main.m_Actors.m_Actors[m_TargetActorID].m_ptPos;
Vector2 velT = main.m_Actors.m_Actors[m_TargetActorID].GetVelocity();
Vector2 totarget = ptT - m_ptLoc;
float a = Vector2.Dot(velT, velT) - (m_VelBullet * m_VelBullet) ;
float b = 2 * Vector2.Dot(velT, totarget);
float c = Vector2.Dot(totarget, totarget);
float p = -b / (2 * a);
float q = (float)Mathf.Sqrt((b * b) - 4 * a * c) / (2 * a);
float t1 = p - q;
float t2 = p + q;
float t;
if (t1 > t2 && t2 > 0)
{
t = t2;
}
else
{
t = t1;
}
Vector2 aimSpot = ptT + (velT * t);
Vector2 bulletPath = aimSpot - m_ptLoc;
// unused as yet...
// float ticksToImpact = bulletPath.magnitude / m_VelBullet;
float target_angle = MaCommon.VectorToAngle(bulletPath);
// move the aim towards target angle...
SetYaw_Funky (MaCommon.SmoothAngle (m_Yaw, target_angle, 20.0f));
After reading about your learning curve grief, I'm staying way from Unity for now ?