74 - Math is important
Alright, finally got that music licensing, so I don't have to mess around with finding music any more. Granted if you have some music you would like me to use, let me know. So today, I mostly worked on getting pathfinding working for the crew combat stuff. I did some other things, but the bulk of it was pathfinding. I also got some of the crew display stuff working for the crew combat section and got selecting working. selecting here works more or less the same as the ship on ship combat. Before we jump into code, let's go over how I did it. Pathfinding might be a wrong term. Honestly I didn't want to deal with it. I knew I didn't want a grid system set up, so that limited what I could do in a reasonable time frame. At first I didn't want anyone to overlap anyone else, but I ended up changing my mind. What happens now is when you target something, a dude will walk at them, once they get within range they will circle around the guy till there is no overlapping dudes. public new void OnTriggerEnter2D(Collider2D other) { base.OnTriggerEnter2D(other); if (other.tag != "Selector" && other.tag != "Ship") { print("enter"); Collisions++; } } public new void OnTriggerExit2D(Collider2D other) { base.OnTriggerExit2D(other); if (other.tag != "Selector" && other.tag != "Ship") { print("Exit"); Collisions--; } if (other.tag == "Ship") { Clockwise = !Clockwise; } } Because it's built into unity, I ended up using the triggering events for collision stuff. Really I just use it to keep track on an int if something is overlapping another dude or not. On the exit method, the whole thing with clockwise we will get into a bit later. But let me say, there is not an easy way to tell if a collider is a subset of another collider. That is to say, if one completely overlaps another. At least I couldn't find a way to figure it out. public void Update() { if (Target != null) { if (!Attacking) Move(); else Attack(); } } Just going to put this here, so you understand how the flow works for every frame. Also I've said it before, but I'll say it again. I hate how the premade methods don't have access modifiers on them. Don't use default shit, jeez. private void Move() { var pos = transform.localPosition; var targetPos = Target.transform.localPosition; var speed = Speed * Time.deltaTime; var xDiff = targetPos.x - pos.x; var yDiff = targetPos.y - pos.y; var total = Mathf.Abs(xDiff) + Mathf.Abs(yDiff); pos.x += (xDiff / total) * speed; pos.y += (yDiff / total) * speed; transform.localPosition = pos; if (Vector3.Distance(targetPos, pos) < AttackRange) { Attacking = true; Vector2 dir = pos - targetPos; dir = transform.InverseTransformDirection(dir); Rotation = Mathf.Atan2(dir.y, dir.x); } } If a dude is not attacking and it has a target, it needs to move, simple enough. We just want to move in a straight line at the guy. The math is to figure out how much to go in the y direction and how much in the x direction. So we have something like (Difference of X) / (ABS difference of X + ABS difference of Y) to figure out the percent of what way we will move. So because we are keeping the sign of the numerator, that lets us know to go left/right or up/down, then because we are dividing by the ABS total, we are getting a percent of how much X or Y to go. I thought it was a clever way to go about it. Then once we move we check if we are within attacking range. If we are we need to set attacking to true and get the rotation figured out. We use the rotation to figure out what angle we are compared to the target. private void GoAround() { var origin = Target.transform.localPosition; var radius = Vector3.Distance(origin, transform.localPosition); if (Clockwise) Rotation -= (Speed / radius) * Time.deltaTime; else Rotation += (Speed / radius) * Time.deltaTime; var cos = Mathf.Cos(Rotation) * radius; var sin = Mathf.Sin(Rotation) * radius; origin.x += cos; origin.y += sin; transform.localPosition = new Vector3(origin.x, origin.y, -1); } The attack method does pretty much nothing right now. It just calls GoAround if the collisions is higher then 0. This is where the clockwise bool comes into play. This pile of math is used to just go around in a circle around the target. We need a few things to figure out where to go: The origin, the target position. The radius, the distance between the target and the attacker. The angle(Rotation), what angle we want to be in the circle. With all that we just need to do some math for the X and Y. As we as adding onto Rotation so it keeps moving. For X it's just origin.X + Cos(angle)*radius, and for Y it's the same but Sin. Once we get that figured out we just set the position. If you don't understand how this works, let me give a quick run down. Cosine and sine are waves that deal with the unit circle. So with cosine cos(0)=1, cos(pi)=-1, cos(2pi)=1, and so on. With sine, sin(pi/2)=1, sin(3pi/2)=-1, and so on. So with a given angle we know where on the unit circle something is, so with angle 0 we are at y=0 x=1, or the leftmost side of a circle. Knowing that we just need to multiply by the radius and add it to the origin to get where we are. Trigonometry! I think that's enough for today. Hope you enjoy math!
2/14/2017 8:04:10 PM





Add Comment Auther

Comment