# Tower Defence Day 3

902 views

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:

1. Enemies turning to face the direction they are running
2. Firing bullets (or projectiles) from guns
3. Aiming system for the towers
4. Healthbars for the enemies
5. Enemy alive / dead states and respawn.
6. 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));

Nice update!

2 minutes ago, Awoken said:

This is why in most cases I prefer just coding my own engines.

Haha it is frustrating but even with that I have to admit to getting a lot working quickly. It's kind of like a visual basic for 3d, but less intuitive lol. Rapid development, but I suspect when it comes to optimizing things, you are somewhat screwed. But it's a fair tradeoff in many cases.

27 minutes ago, Rutin said:

This is why in most cases I prefer just coding my own engines.

100%, a lot of the tricks I've pulled off I don't think I could get away with in an existing engine.

5 minutes ago, Awoken said:

100%, a lot of the tricks I've pulled off I don't think I could get away with in an existing engine.

This is why I've mainly used in house engines.  I just like having that control, and being able to add and change things on the fly.

Awesome development for the challenge bro ^ _^ y ...

"Another day with more time spent wrestling with Unity rather than writing much code "

I'm on the same boat experience with Unity/Unreal , the reason I'm concentrating on MonoGame it's a code centric game framework ^_^Y.

It sucks spending time fighting with your tech rather than getting things done! I'm sure it does get easier as you become more familiar with the engine though!

## Create an account

Register a new account