54 - Weapon targeting finished
So today I got targeting all working! The basic idea is that when a weapon is targeting something an image is put onto whatever it's targeting, each weapon that is targeting something will have it's own image, but it's fine because they will overlap and just numbers will show, and that's the important part. So I also added some code that shows when you have a weapon selected, basicly it will just change the outline when it's selected so you can tell what you are aiming with, now on to the targeting code. void OnMouseDown() { if (transform.tag == "Player") { //stuff } else { if (Ship.OtherShip.WeaponSelecting) { var weapon = Ship.OtherShip.Targeting; if(weapon.Target != null) { weapon.UnTarget(); } weapon.Target = this; Targeted(weapon); Ship.OtherShip.Targeting.WeaponUnselected(); } } } Alright so this is the code that deals with what happens when you click on something while you are currently selecting with a weapon. It grabs the weapon that is currently targeting, if the weapons target isn't null we call a method to untarget it's old target, then we set the new target and make it so we stop selecting a weapon. I'm not going to show the Targeted method, all that does is creates a new game object and adds an image too it. But lets look at the UnTarget code. Oh also in the Targeted method, it checks if the weapon is currently added to the Targetings List from last time, and adds it if it isn’t. The only time we remove a weapon from the Targetings list is when you right click, because it’s hold onto the weapon, if the weapon changes target it doesn’t matter. public void UnTarget() { if (TargetingType == typeof(Segment)) ((Segment)Target).UnTargeted(this); if (TargetingType == typeof(Connector)) ((Connector)Target).UnTargeted(this); } So this is in the weapons class. Really all it does is checks the TargetingType wish is of type Type, to see if the target is a segment or a connector. And depending on that it calls an UnTargeted method, really both methods do basicly the same thing. public void UnTargeted(Weapons weapon) { var temp = Targets.First(x => x.name.Contains(weapon.PosOfSystem + "")); Targets.Remove(temp); Destroy(temp); } So to unTarget we just grab the game object with the image out of a list we hold in each segment/connector, remove it from the list, and remove it from the game, done simple. if (WeaponSelecting) { if (Targetings.Contains(Targeting)) { Targetings.Remove(Targeting); } if (Targeting.Target != null) { if (Targeting.TargetingType == typeof(Segment)) ((Segment)Targeting.Target).UnTargeted(Targeting); if (Targeting.TargetingType == typeof(Connector)) ((Connector)Targeting.Target).UnTargeted(Targeting); } Targeting.Target = null; Targeting.WeaponUnselected(); } So this is in the Ship Class under the Update method if you right click. I showed part of this yesterday, and I told you we would be changing it, and here we go. Really the main thing to look at here, is if the currently selected weapon's target is not null, we have to untarget the previous target. Really I think that's it. It wasn't really two complex, I was able to finish this and a few other things within two hours. This doesn't cover everything I did either, granted this was a majority of it. Oh Also something I should note, stream tomorrow is going to happen later in the day then normal.
1/26/2017 10:23:27 PM

Add Comment Auther