75 - Crew combat animation
So yesterday I went over how I did the pathfinding. Today, I got that all ironed out, added some animations for walking, and tweaked a few other things. So I'll be covering in detail how I got animations working, but let's quickly go over some other things I did. So I got selecting units with function keys to work, I basically copied over the code from before, so nothing new there. Also I got it so instead of having to click the sprite to target, you can also click on the little unit display. Figured it would be nice to be able to also click something that isn't moving. private void Attack() { var pos = transform.localPosition; var targetPos = Target.transform.localPosition; if (Vector3.Distance(targetPos, pos) < AttackRange) { if (AttackCounter >= _AttackTime) { AttackCounter = 0f; Target.TakeDamage(Damage); } else AttackCounter += Time.deltaTime; if (Spin) GoAround(); } else { Attacking = false; } } So this is the code for attacking, really not much to it. First we just check to make sure we are still within the attack range. If we are then we start a counter and do damage once the counter hits AttackTime. Oh I changed how the whole spinning thing works, well kinda. Instead of checking if there are any collisions I check this Spin boolean. I changed it so once you are in place, you won't start moving if another dude walks on top of you. You shouldn't move once you start attacking, assuming your target doesn't move and you have stopped moving yourself. private void StepBack() { var origin = Target.transform.localPosition; var cos = Mathf.Cos(Rotation); var sin = Mathf.Sin(Rotation); var speed = _Speed * Time.deltaTime; var pos = transform.localPosition; XMove = cos * speed; YMove = sin * speed; pos.x += XMove; pos.y += YMove; transform.localPosition = pos; if(Vector3.Distance(origin, pos) > MinAttackRange) { Attacking = true; MoveBack = false; Spin = Collisions > 0; } } So this is another part of movement. If we get to close to the target, we want to take a step back before we start spinning and/or attacking. Because we already have the Rotation we just figure out need to back out that way. Nothing to complex compared to some of the math yesterday. private void FigureOutDirection() { if (YMove == 0 && XMove == 0) { if (Attacking) DirectionOfAttack(false); } else { if (Attacking) DirectionOfAttack(true); else { var direction = ""; if (Mathf.Abs(XMove) >= Mathf.Abs(YMove)) { if (XMove > 0) direction = "Right"; else direction = "Left"; } else { if (YMove > 0) direction = "Up"; else direction = "Down"; } if (Direction != direction) { Direction = direction; CrewAnimation(Direction); } } } } In that last method, StepBack, you might have noticed XMove and YMove, we I'm using that to figure out what animation to use. Because we have 3 different movement methods that could be called, instead of copying code I just save how much we moved in the X and Y direction and call this method at the end up update. Really all this is doing is figuring out what way to face. If abs(X) is bigger than abs(Y) we are moving left or right, otherwise we are moving up or down. Next we check if it is negative or not. So really just two if statements, nice. private void DirectionOfAttack(bool Moving) { var cos = Mathf.Cos(Rotation); var sin = Mathf.Sin(Rotation); var direction = ""; if(Mathf.Abs(cos) >= Mathf.Abs(sin)) { if (cos > 0) direction = "Left"; else direction = "Right"; } else { if (sin > 0) direction = "Down"; else direction = "Up"; } if (Moving) { if(Direction != direction) { Direction = direction; CrewAnimation(Direction); } } else { //attack animation in direction } } So this is the other half of the animation. If we are spinning around the target we want to face the target instead of facing the direction we are heading. All we do is grab the cos and sin of the rotation and that gives us the x and y of the unit circle, and basicly do the same here as before. But it is different enough that we needed to write new code for it. I won't go into detail, try to figure this out yourself. But let me set it up for you. cos(angle) is your x value, 1 - -1, and sin(angle) is your y value 1 - -1. Think about that and at the same time imagine the unit circle. Alright I guess that is enough code for today. Things are coming together nicely. Remember if you want to play the next build you need to hit the first reward teir on my patreon. I will be putting out a build every month, and I'll only do a public build every now and then.
2/15/2017 7:07:27 PM

Add Comment Auther