private void RemoveBeam(TBeam b) { b.TractorSound.audio.Stop(); b.TractorSound.audio.enabled = false; //foo1.removeGimbal(); foo2.removeGimbal(); //foo4.removeGimbal(); UnityEngine.Debug.Log("ImpulseDrive TractorBeam: RemoveBeam 1 : " + b.ship.vesselName); BeamDrawer BD1 = b.BeamDrawer1; LineRenderer LR1 = b.LineRenderer1; BeamDrawer BD2 = b.BeamDrawer2; LineRenderer LR2 = b.LineRenderer2; BeamDrawer debug_BD1 = b.debug1_BeamDrawer; LineRenderer debug_LR1 = b.debug1_LineRenderer; BeamDrawer debug_BD2 = b.debug2_BeamDrawer; LineRenderer debug_LR2 = b.debug2_LineRenderer; UnityEngine.Debug.Log("ImpulseDrive TractorBeam: RemoveBeam 2 : " + b.ship.vesselName); UnityEngine.Debug.Log("ImpulseDrive TractorBeam: RemoveBeam 3 : " + b.ship.vesselName); //b.debug1_LineRenderer.enabled = false; UnityEngine.Debug.Log("ImpulseDrive TractorBeam: RemoveBeam 4 : " + b.ship.vesselName); LR1 = BD1.render(Vector3.zero, Color.blue, 1); LR1.SetWidth(0, 0); LR1.SetPosition(0, Vector3.zero); LR1.SetPosition(1, Vector3.zero); //BD1.Destroy(); UnityEngine.Debug.Log("ImpulseDrive TractorBeam: RemoveBeam 5 : " + b.ship.vesselName); Color fc = new Color(0.469F, 0.484F, 0.371F, 0.8F); LR2 = BD2.render(Vector3.zero, fc, 1); LR2.SetWidth(0, 0); LR2.SetPosition(0, Vector3.zero); LR2.SetPosition(1, Vector3.zero); //BD2.Destroy(); UnityEngine.Debug.Log("ImpulseDrive TractorBeam: RemoveBeam 6 : " + b.ship.vesselName); /* * debug_LR1 = debug_BD1.render(Vector3.zero, Color.green); * debug_LR1.SetWidth(0, 0); * debug_LR1.SetPosition(0, Vector3.zero); * debug_LR1.SetPosition(1, Vector3.zero); * //debug_BD1.Destroy(); * UnityEngine.Debug.Log("ImpulseDrive TractorBeam: RemoveBeam 7 : " + b.ship.vesselName); * * debug_LR2 = debug_BD2.render(Vector3.zero, Color.yellow); * debug_LR2.SetWidth(0, 0); * debug_LR2.SetPosition(0, Vector3.zero); * debug_LR2.SetPosition(1, Vector3.zero); * //debug_BD2.Destroy(); * UnityEngine.Debug.Log("ImpulseDrive TractorBeam: RemoveBeam 8 : " + b.ship.vesselName); */ this.BeamIDList.Remove(b.beamID); this.BeamList.Remove(b); //BD = null; //LR = null; //b = null; UnityEngine.Debug.Log("ImpulseDrive TractorBeam: RemoveBeam 9 : " + b.ship.vesselName); }
//GimbalDebug foo4 = new GimbalDebug(); internal void CreateBeam(Vessel v) { UnityEngine.Debug.Log("ImpulseDrive TractorBeam: CreateBeam 1 "); Vessel Parent = FlightGlobals.ActiveVessel; //Vector3d parentPosition_current = Parent.findWorldCenterOfMass(); //Vector3d childPosition_current = v.findWorldCenterOfMass(); Vector3d parentPosition_current = Parent.GetWorldPos3D(); Vector3d childPosition_current = v.GetWorldPos3D(); float distance = Vector3.Distance(parentPosition_current, childPosition_current); Vector3 newVector = childPosition_current - parentPosition_current; //UnityEngine.Debug.Log("ImpulseDrive TractorBeam: CreateBeam 2 "); TBeam nb = new TBeam(); UnityEngine.Debug.Log("ImpulseDrive TractorBeam: CreateBeam 3-7 "); BeamDrawer BD1 = new BeamDrawer(); LineRenderer LR1 = new LineRenderer(); BeamDrawer BD2 = new BeamDrawer(); LineRenderer LR2 = new LineRenderer(); BeamDrawer debug_BD1 = new BeamDrawer(); LineRenderer debug_LR1 = new LineRenderer(); BeamDrawer debug_BD2 = new BeamDrawer(); LineRenderer debug_LR2 = new LineRenderer(); nb.beamID = v.id.ToString(); nb.ship = v; nb.ShipPosition = childPosition_current; nb.ShipRotation = nb.ship.transform.rotation; nb.ParentPosition = parentPosition_current; nb.ParentRotation = Parent.transform.rotation; nb.force = 0.5F; nb.distance = distance; nb.Empty1 = new GameObject("Empty1"); nb.Empty2 = new GameObject("Empty2"); /* */ nb.ship.GoOffRails(); //foo1.drawGimbal(nb.Empty1, 10, 1f); foo2.drawGimbal(nb.Empty2, 12, 0.5f); //foo4.drawGimbal(childPosition_current, 20, 0.5f); //nb.Empty2.transform.parent = nb.Empty1.transform; //nb.Empty2.transform.rotation = FlightGlobals.ActiveVessel.transform.rotation; nb.Vector = newVector; nb.y_slider = nb.yAngle; nb.z_slider = nb.zAngle; nb.x_slider = nb.xAngle; nb.BeamDrawer1 = BD1; nb.LineRenderer1 = LR1; nb.BeamDrawer2 = BD2; nb.LineRenderer2 = LR2; nb.debug1_BeamDrawer = debug_BD1; nb.debug1_LineRenderer = debug_LR1; nb.debug2_BeamDrawer = debug_BD2; nb.debug2_LineRenderer = debug_LR2; nb.hold_distance = true; nb.hold_Vector = true; UnityEngine.Debug.Log("ImpulseDrive WeaponSystems: CreateTarget TractorSound "); nb.TractorSound = new FXGroup("TractorSound"); GameObject audioObj = new GameObject(); nb.audioObj = audioObj; nb.audioObj.transform.position = FlightGlobals.ActiveVessel.transform.position; nb.audioObj.transform.parent = FlightGlobals.ActiveVessel.transform; // add to parent nb.TractorSound.audio = audioObj.AddComponent <AudioSource>(); nb.TractorSound.audio.dopplerLevel = 0f; nb.TractorSound.audio.Stop(); nb.TractorSound.audio.clip = GameDatabase.Instance.GetAudioClip(TractorSoundFile); nb.TractorSound.audio.loop = true; // Seek to a random position in the sound file so we don't have // harmonic effects when burning at multiple RCS nozzles. nb.TractorSound.audio.time = UnityEngine.Random.Range(0, nb.TractorSound.audio.clip.length); nb.TractorSound.audio.Play(); nb.TractorSound.audio.enabled = false; try { if (nb.TractorSound != null && nb.TractorSound.audio != null) { nb.soundVolume = GameSettings.SHIP_VOLUME * TractorSoundVolume; nb.TractorSound.audio.enabled = true; nb.TractorSound.audio.volume = nb.soundVolume; } } catch (Exception ex) { Debug.LogError("TractorSound Error OnUpdate: " + ex.Message); } this.BeamList.Add(nb); this.BeamIDList.Add(nb.beamID); nb = null; BD1 = null; LR1 = null; BD2 = null; LR2 = null; debug_BD1 = null; debug_LR1 = null; debug_BD2 = null; debug_LR2 = null; childPosition_current = Vector3.zero; }
public void Update_RenderBeams() { foreach (TBeam b in this.BeamList) { Vessel Parent = FlightGlobals.ActiveVessel; Vector3 parentPosition_current = Parent.GetWorldPos3D(); Vector3 childPosition_current = b.ship.GetWorldPos3D(); //Vector3 parentPosition_current = Parent.findWorldCenterOfMass(); //Vector3 childPosition_current = b.ship.findWorldCenterOfMass(); //Vector3 parentPosition_current = Parent.findLocalCenterOfMass(); //Vector3 childPosition_current = b.ship.findLocalCenterOfMass(); //Vector3 parentPosition_current = Parent.transform.position; //Vector3 childPosition_current = b.ship.transform.position; //Vector3 parentPosition_current = Parent.transform.localPosition; //Vector3 childPosition_current = b.ship.transform.localPosition; BeamDrawer BD1 = b.BeamDrawer1; LineRenderer LR1 = b.LineRenderer1; BeamDrawer BD2 = b.BeamDrawer2; LineRenderer LR2 = b.LineRenderer2; BeamDrawer debug_BD1 = b.debug1_BeamDrawer; LineRenderer debug_LR1 = b.debug1_LineRenderer; BeamDrawer debug_BD2 = b.debug2_BeamDrawer; LineRenderer debug_LR2 = b.debug2_LineRenderer; GameObject Empty1 = b.Empty1; GameObject Empty2 = b.Empty2; int width = 0; float check_distance = Vector3.Distance(parentPosition_current, childPosition_current); if (check_distance < 2000F) { b.ship.GoOffRails(); //* works!!!! // MOVE THE HELPER LINE - NO FORCE APPLIED HERE Empty1.transform.parent = Parent.transform; Empty1.transform.position = Parent.transform.position; Empty2.transform.parent = Empty1.transform; Empty2.transform.position = Empty1.transform.position; Empty2.transform.rotation = Empty1.transform.rotation; Vector3 temp2 = Empty2.transform.TransformDirection(b.Vector); Vector3 GimbalPosition = SetVectorLength(temp2, b.distance); //Empty2.transform.position = GimbalPosition; Empty2.transform.localPosition = GimbalPosition; Empty1.transform.Rotate(b.x_slider, 0, b.z_slider); b.xAngle = Empty1.transform.localEulerAngles.x; b.zAngle = Empty1.transform.localEulerAngles.z; b.x_slider = 0; b.y_slider = 0; b.z_slider = 0; // MOVE THE HELPER LINE - NO FORCE APPLIED HERE // OBSOLETE - RENDER THE HELPER LINE - NO FORCE APPLIED HERE b.distance = Mathf.Clamp(b.distance, 5, 2000); b.debug1_LineRenderer = debug_BD1.render2(Empty1.transform, Empty1.transform.up, Empty1.transform.localPosition, b.distance, 0.1f, Color.green, Color.red); b.debug1_LineRenderer.enabled = false; // OBSOLETE - RENDER THE HELPER LINE - NO FORCE APPLIED HERE // works !!!! */ // FORCE APPLIED HERE //Vector3 newVector = SetVectorLength(Empty1.transform.TransformPoint(Empty1.transform.up), b.distance); //float current_Distance = Vector3.Distance(parentPosition_current, childPosition_current); //float step = current_Distance * Time.deltaTime; //b.ship.transform.position = Vector3.Lerp(childPosition_current, Empty2.transform.position, 0.5F * Time.deltaTime); //b.ship.transform.position = Vector3.Lerp(b.ship.transform.position, Empty2.transform.position, 0.5F * Time.deltaTime); float targetError = Vector3.Distance(b.ship.transform.position, Empty2.transform.position); b.targetError = targetError; float step = targetError * Time.deltaTime; b.ship.transform.position = Vector3.MoveTowards(b.ship.transform.position, Empty2.transform.position, step); Vector3 targetErrorVector = b.ship.transform.position - Empty2.transform.position; foreach (var vesselPart in b.ship.parts.Where(p => p.rigidbody != null)) { vesselPart.rigidbody.AddForce(targetErrorVector * -1 * (b.force / targetError), ForceMode.Acceleration); } // FORCE APPLIED HERE /* */ // RENDER THE TRACTOR BEAM - NO FORCE APPLIED HERE Vector3 TractorBeamVector = b.Vector; System.Random rnd = new System.Random(); width = rnd.Next(5, 12); LR1 = BD1.render(TractorBeamVector, Color.blue, 1); LR1.SetWidth(1, width); LR1.SetPosition(0, FlightGlobals.ActiveVessel.findWorldCenterOfMass()); LR1.SetPosition(1, b.ship.GetWorldPos3D()); Color fc = new Color(0.469F, 0.484F, 0.371F, 0.8F); width = rnd.Next(1, 5); LR2 = BD2.render(TractorBeamVector, fc, 1); LR2.SetWidth(1, width); LR2.SetPosition(0, FlightGlobals.ActiveVessel.findWorldCenterOfMass()); LR2.SetPosition(1, b.ship.GetWorldPos3D()); // RENDER THE TRACTOR BEAM - NO FORCE APPLIED HERE } else { this.RemoveBeam(b); } } }