🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Tower Defence Day 3

Published May 07, 2018
Advertisement

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

healthbars.jpg.4bc4766b7b0ba9dd17cef0b88a6f03c3.jpg

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));

 

3 likes 7 comments

Comments

Awoken

After reading about your learning curve grief, I'm staying way from Unity for now ?   

May 07, 2018 07:48 PM
Rutin

Nice update! :D 

 

2 minutes ago, Awoken said:

After reading about your learning curve grief, I'm staying way from Unity for now

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

May 07, 2018 07:51 PM
lawnjelly

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. :)

May 07, 2018 07:53 PM
Awoken
27 minutes ago, Rutin said:

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

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

May 07, 2018 08:18 PM
Rutin
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.

May 07, 2018 08:37 PM
DexterZ101

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.

 

May 08, 2018 10:28 AM
jbadams

 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! :)

May 09, 2018 01:55 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement