Jump to content
  • Advertisement

Tower Defence Day 3

lawnjelly

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

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

 



7 Comments


Recommended Comments

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

Share this comment


Link to comment

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 

Share this comment


Link to comment

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

Share this comment


Link to comment
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.

Share this comment


Link to comment
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.

Share this comment


Link to comment

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.

 

Share this comment


Link to comment

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

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!