public void FixedUpdate() { Events["ActivateStrut"].guiActiveEditor = Events["ActivateStrut"].active = !IsEnabled; Events["DeactivateStrut"].guiActiveEditor = Events["DeactivateStrut"].active = IsEnabled; if (IsEnabled) { I = I + 1 % 255; if (strut == null || strut.isDestroyed) { if ((I % 10) == 0) { CheckHit(); } } } else { if (strut != null) { strut.Destroy(); strut = null; } } base.OnUpdate(); }
void CheckHit() { if (HighLogic.LoadedSceneIsEditor) { Logging.PostDebugMessage(this, "Checking bailing out: in the editor!"); return; } if (!isEnabled) { Logging.PostDebugMessage(this, "Destroying strut."); strut.Destroy(); strut = null; return; } Logging.PostDebugMessage(this, "Checking for ray hit."); Logging.PostDebugMessage(this, "Enabled, continuing."); if (strut == null || strut.isDestroyed) { Logging.PostDebugMessage(this, "strut is {0}", strut == null ? "null" : strut.isDestroyed.ToString()); Vector3 dir = getTransform().TransformDirection(Dir); Vector3 start = getTransform().TransformPoint(Start); Logging.PostDebugMessage(this, "Got transforms. Checking for raycast hit."); UnityEngine.RaycastHit info = new RaycastHit(); bool hit = Physics.Raycast(new UnityEngine.Ray(start + (dir * 0.05f), dir), out info, MaxStrutLength); if (hit) { Logging.PostDebugMessage(this, "Found raycast hit. Fetching target part."); Part targetPart = Util.partFromRaycast(info); Logging.PostDebugMessage(this, "Found target part {0} on {1}.", targetPart.partName, targetPart.vessel == null ? "null vessel" : targetPart.vessel.vesselName ); if ( targetPart && vessel.parts.Contains(targetPart) && Util.GetEnergy(part.vessel) > 5 * TimeWarp.fixedDeltaTime ) { Logging.PostDebugMessage(this, "Target part is in our vessel and we have the energy to continue."); strut = new Strut( part, targetPart, targetPart.transform.InverseTransformPoint(info.point), getTransform() ); Logging.PostDebugMessage(this, "Built a new strut, setting material, colors, and sizes."); strut.Material = material; strut.StartColor = startColor; strut.EndColor = endColor; strut.StartSize = StartSize; strut.EndSize = EndSize; Logging.PostDebugMessage(this, "Strut all done!"); } } } }