private void AttachOnCollision(Collision collision) { //Don't attach if inpact force is too low if (collision.relativeVelocity.magnitude < forceNeeded) { return; } float shorterDist = Mathf.Infinity; bool nearestHitFound = false; Part nearestHitPart = null; RaycastHit nearestHit = new RaycastHit(); Vector3 rayDirection = this.part.transform.TransformDirection(rayDir); //Get all raycast hits in front of the grapple List <RaycastHit> nearestHits = new List <RaycastHit>(Physics.RaycastAll(this.part.transform.position, rayDirection, rayLenght, 557059)); foreach (RaycastHit hit in nearestHits) { //Exclude grapple collider if (hit.collider == this.part.collider) { continue; } //Exclude parts if needed if (!attachToPart) { if (hit.rigidbody) { if (hit.rigidbody.GetComponent <Part>()) { continue; } } } /* * // Check forward dot * float fwdDot = Mathf.Abs(Vector3.Dot(hit.normal, this.transform.up)); * if (fwdDot <= minFwdDot) * { * continue; * } * * // Check roll dot * float rollDot = Vector3.Dot(hit.normal, this.transform.up); * if (rollDot <= minRollDot) * { * continue; * }*/ // Get closest hit float tmpShorterDist = Vector3.Distance(this.part.transform.position, hit.point); if (tmpShorterDist <= shorterDist) { shorterDist = tmpShorterDist; nearestHit = hit; if (nearestHit.rigidbody) { nearestHitPart = nearestHit.rigidbody.GetComponent <Part>(); } nearestHitFound = true; } } if (!nearestHitFound) { KAS_Shared.DebugLog("AttachOnCollision - Nothing to attach in front of grapple"); return; } KASModuleWinch connectedWinch = KAS_Shared.GetConnectedWinch(this.part); if (connectedWinch) { MoveAbove(nearestHit.point, nearestHit.normal, aboveDist); connectedWinch.cableJointLength = connectedWinch.cableRealLenght; } if (nearestHitPart) { KAS_Shared.DebugLog("AttachOnCollision - grappleAttachOnPart=true"); KAS_Shared.DebugLog("AttachOnCollision - Attaching to part : " + nearestHitPart.partInfo.title); AttachPartGrapple(nearestHitPart, partBreakForce); } else { KAS_Shared.DebugLog("AttachOnCollision - Attaching to static : " + nearestHit.collider.name); AttachStaticGrapple(staticBreakForce); } }
public void UpdatePointer() { if (!running) { if (pointer) { UnityEngine.Object.Destroy(pointer); } return; } //Cast ray Ray ray = FlightCamera.fetch.mainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (!Physics.Raycast(ray, out hit, 500, 557059)) { if (pointer) { UnityEngine.Object.Destroy(pointer); } return; } //Create pointer if needed if (!pointer) { GameObject modelGo = partToAttach.FindModelTransform("model").gameObject; pointer = Mesh.Instantiate(modelGo) as GameObject; foreach (Collider col in pointer.GetComponentsInChildren <Collider>()) { UnityEngine.Object.Destroy(col); } allModelMr = new List <MeshRenderer>(); // Remove attached tube mesh renderer if any List <MeshRenderer> tmpAllModelMr = new List <MeshRenderer>(pointer.GetComponentsInChildren <MeshRenderer>() as MeshRenderer[]); foreach (MeshRenderer mr in tmpAllModelMr) { if (mr.name == "KAStube" || mr.name == "KASsrcSphere" || mr.name == "KASsrcTube" || mr.name == "KAStgtSphere" || mr.name == "KAStgtTube") { Destroy(mr); continue; } allModelMr.Add(mr); mr.material = new Material(Shader.Find("Transparent/Diffuse")); } pointerNodeTransform = new GameObject("KASPointerPartNode").transform; pointerNodeTransform.parent = pointer.transform; pointerNodeTransform.localPosition = partToAttach.srfAttachNode.position; pointerNodeTransform.localRotation = Quaternion.Inverse(Quaternion.LookRotation(partToAttach.srfAttachNode.orientation, Vector3.up)); } //Set default color Color color = Color.green; // Check if object is valid bool isValidObj = false; Part hitPart = null; KerbalEVA hitEva = null; if (hit.rigidbody) { hitPart = hit.rigidbody.GetComponent <Part>(); hitEva = hit.rigidbody.GetComponent <KerbalEVA>(); if (hitPart && allowPart && !hitEva & hitPart != partToAttach) { isValidObj = true; } if (hitEva && allowEva) { isValidObj = true; } } if (!hitPart && !hitEva && allowStatic) { isValidObj = true; } //Check distance bool isValidSourceDist = true; if (sourceTransform) { isValidSourceDist = Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, sourceTransform.position) <= maxDist; } bool isValidTargetDist = Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, hit.point) <= maxDist; //Set color if (!isValidObj) { color = Color.red; } else if (!isValidSourceDist || !isValidTargetDist) { color = Color.yellow; } color.a = 0.5f; foreach (MeshRenderer mr in allModelMr) { mr.material.color = color; } //Rotation keys Vector3 delta = new Vector3(0, 0, 1); if (Input.GetKey(KeyCode.LeftAlt)) { delta = new Vector3(1, 0, 0); } else if (Input.GetKey(KeyCode.RightAlt)) { delta = new Vector3(0, -1, 0); } if (Input.GetKeyDown(KASAddonControlKey.rotateLeftKey.ToLower())) { customRot -= delta * 15; } if (Input.GetKeyDown(KASAddonControlKey.rotateRightKey.ToLower())) { customRot += delta * 15; } Quaternion rotAdjust = Quaternion.Euler(0, 0, customRot.z) * Quaternion.Euler(customRot.x, customRot.y, 0); KAS_Shared.MoveAlign(pointer.transform, pointerNodeTransform, hit, rotAdjust); //Attach on click if (Input.GetKeyDown(KeyCode.Mouse0)) { KAS_Shared.DebugLog("Attachment started..."); if (!isValidObj) { ScreenMessages.PostScreenMessage("Can't attach, target is not allowed !"); audioBipWrong.Play(); return; } if (!isValidSourceDist) { ScreenMessages.PostScreenMessage("Can't attach, too far from source !"); audioBipWrong.Play(); return; } if (!isValidTargetDist) { ScreenMessages.PostScreenMessage("Can't attach, too far from target !"); audioBipWrong.Play(); return; } KASModuleGrab modulegrab = partToAttach.GetComponent <KASModuleGrab>(); //Move and attach mode if (pointerMode == PointerMode.MoveAndAttach) { // Drop and detach part if needed if (modulegrab) { if (modulegrab.grabbed) { modulegrab.Drop(); } modulegrab.Detach(); } KASModuleWinch connectedWinch = KAS_Shared.GetConnectedWinch(partToAttach); if (!connectedWinch) { KAS_Shared.DecoupleFromAll(partToAttach); } //Move part partToAttach.transform.position = pointer.transform.position; partToAttach.transform.rotation = pointer.transform.rotation; if (connectedWinch) { //Set cable lenght to real lenght connectedWinch.cableJointLength = connectedWinch.cableRealLenght; } KAS_Shared.ResetCollisionEnhancer(partToAttach); if (msgOnly) { KAS_Shared.DebugLog("UpdatePointer(Pointer) Attach using send message"); if (hitPart) { partToAttach.SendMessage("OnAttachPart", hitPart, SendMessageOptions.DontRequireReceiver); } else { partToAttach.SendMessage("OnAttachStatic", SendMessageOptions.DontRequireReceiver); } } else { KAS_Shared.DebugLog("UpdatePointer(Pointer) Attach with couple or static method"); if (!hitPart && !hitEva) { if (modulegrab) { modulegrab.AttachStatic(); modulegrab.fxSndAttachStatic.audio.Play(); } else { KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, part cannot be attached on static"); } } else { partToAttach.Couple(hitPart); if (modulegrab) { modulegrab.fxSndAttachPart.audio.Play(); } else { KAS_Shared.DebugWarning("UpdatePointer(Pointer) No grab module found, cannot fire sound"); } } partToAttach.SendMessage("OnAttach", SendMessageOptions.DontRequireReceiver); } } if (pointerMode == PointerMode.CopyAndAttach) { // Not tested ! Part newPart = KAS_Shared.CreatePart(partToAttach.partInfo, pointer.transform.position, pointer.transform.rotation, partToAttach); if (msgOnly) { if (hitPart) { StartCoroutine(WaitAndSendMsg(newPart, pointer.transform.position, pointer.transform.rotation, hitPart)); } else { StartCoroutine(WaitAndSendMsg(newPart, pointer.transform.position, pointer.transform.rotation)); } } else { if (!hitPart && !hitEva) { StartCoroutine(WaitAndAttach(newPart, pointer.transform.position, pointer.transform.rotation, hitPart)); } else { StartCoroutine(WaitAndAttach(newPart, pointer.transform.position, pointer.transform.rotation)); } } } running = false; } }
public void Grab(Vessel kerbalEvaVessel) { KAS_Shared.DebugLog("Grab - Grabbing part :" + this.part.partInfo.name); //Get eva transform evaCollider = KAS_Shared.GetEvaCollider(kerbalEvaVessel, evaTransformName); if (!evaCollider) { KAS_Shared.DebugLog("Grab - " + evaTransformName + "transform not found on eva !"); return; } //Get attach node if (attachNodeName == null || attachNodeName == "") { if (this.part.srfAttachNode == null) { KAS_Shared.DebugLog("Grab - surface attach node cannot be found on the part !"); return; } KAS_Shared.AddNodeTransform(this.part, this.part.srfAttachNode); partNode = this.part.srfAttachNode; } else { AttachNode an = this.part.findAttachNode(attachNodeName); if (an == null) { KAS_Shared.DebugLog("Grab - " + attachNodeName + " node cannot be found on the part !"); return; } KAS_Shared.AddNodeTransform(this.part, an); partNode = an; } //Send message to other modules base.SendMessage("OnPartGrab", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver); //Drop grabbed part on eva if needed KASModuleGrab tmpGrabbbedPartModule = KAS_Shared.GetGrabbedPartModule(kerbalEvaVessel); if (tmpGrabbbedPartModule) { KAS_Shared.DebugWarning("Grab - Drop current grabbed part"); tmpGrabbbedPartModule.Drop(); } evaNodeTransform = new GameObject("KASEvaNode").transform; evaNodeTransform.parent = evaCollider.transform; evaNodeTransform.localPosition = evaPartPos; evaNodeTransform.rotation = KAS_Shared.DirectionToQuaternion(evaCollider.transform, evaPartDir); KAS_Shared.MoveAlign(this.part.transform, partNode.nodeTransform, evaNodeTransform); //Grab winch connected head if any KASModuleWinch moduleWinch = KAS_Shared.GetConnectedWinch(this.part); if (moduleWinch) { KASModulePort modulePort = this.part.GetComponent <KASModulePort>(); moduleWinch.UnplugHead(false); moduleWinch.GrabHead(kerbalEvaVessel, modulePort); } List <Collider> allColliders = new List <Collider>(this.part.GetComponentsInChildren <Collider>() as Collider[]); foreach (Collider col in allColliders) { col.isTrigger = true; } Detach(); KAS_Shared.DecoupleFromAll(this.part); this.part.Couple(kerbalEvaVessel.rootPart); //Destroy joint to avoid buggy eva move Destroy(this.part.attachJoint); this.part.rigidbody.velocity = kerbalEvaVessel.rootPart.rigidbody.velocity; if (physicJoint) { if (evaJoint) { Destroy(evaJoint); } evaJoint = this.part.gameObject.AddComponent <FixedJoint>(); evaJoint.connectedBody = evaCollider.attachedRigidbody; evaJoint.breakForce = 5; evaJoint.breakTorque = 5; } else { this.part.physicalSignificance = Part.PhysicalSignificance.NONE; this.part.transform.parent = evaNodeTransform; this.part.rigidbody.isKinematic = true; } //Add grabbed part mass to eva if (addPartMass && !physicJoint) { orgKerbalMass = kerbalEvaVessel.rootPart.mass; kerbalEvaVessel.rootPart.mass += this.part.mass; } evaHolderVesselName = kerbalEvaVessel.vesselName; evaHolderPart = kerbalEvaVessel.rootPart; grabbed = true; RefreshContextMenu(); //Play grab sound fxSndGrab.audio.Play(); base.SendMessage("OnPartGrabbed", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver); }
public void Grab(Vessel kerbalEvaVessel) { KAS_Shared.DebugLog("Grab - Grabbing part :" + this.part.partInfo.name); if (!AlignEvaPosition(kerbalEvaVessel)) { return; } //Grab winch connected head if any KASModuleWinch moduleWinch = KAS_Shared.GetConnectedWinch(this.part); if (moduleWinch) { KASModulePort modulePort = this.part.GetComponent <KASModulePort>(); moduleWinch.UnplugHead(false); moduleWinch.GrabHead(kerbalEvaVessel, modulePort); } keepTriggers = new List <Collider>(); List <Collider> allColliders = new List <Collider>(this.part.GetComponentsInChildren <Collider>() as Collider[]); foreach (Collider col in allColliders) { if (col.isTrigger) { keepTriggers.Add(col); } col.isTrigger = true; } Detach(); if (this.part.vessel != kerbalEvaVessel) { KAS_Shared.DecoupleFromAll(this.part); this.part.Couple(kerbalEvaVessel.rootPart); } else { if (this.part.parent != kerbalEvaVessel.rootPart) { this.part.setParent(null); this.part.setParent(kerbalEvaVessel.rootPart); } this.part.PromoteToPhysicalPart(); } //Destroy joint to avoid buggy eva move Destroy(this.part.attachJoint); this.part.rigidbody.velocity = kerbalEvaVessel.rootPart.rigidbody.velocity; if (physicJoint) { if (evaJoint) { Destroy(evaJoint); } evaJoint = this.part.gameObject.AddComponent <FixedJoint>(); evaJoint.connectedBody = evaCollider.attachedRigidbody; evaJoint.breakForce = 5; evaJoint.breakTorque = 5; KAS_Shared.ResetCollisionEnhancer(this.part); } else { this.part.physicalSignificance = Part.PhysicalSignificance.NONE; this.part.transform.parent = evaNodeTransform; this.part.rigidbody.isKinematic = true; KAS_Shared.ResetCollisionEnhancer(this.part, false); } //Add grabbed part mass to eva if (addPartMass && !physicJoint) { orgKerbalMass = kerbalEvaVessel.rootPart.mass; kerbalEvaVessel.rootPart.mass += this.part.mass; } GameEvents.onCrewBoardVessel.Add(new EventData <GameEvents.FromToAction <Part, Part> > .OnEvent(this.OnCrewBoardVessel)); evaHolderVesselName = kerbalEvaVessel.vesselName; evaHolderPart = kerbalEvaVessel.rootPart; grabbed = true; grab_pending = false; RefreshContextMenu(); //Play grab sound fxSndGrab.audio.Play(); base.SendMessage("OnPartGrabbed", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver); }
private void AttachOnCollision(Collision collision) { // Revert precision mode back to the performance. Non-discrete modes are very expensive. KAS_Shared.DebugLog(string.Format( "AttachOnCollision - Set collision mode back to Discrete on: {0}", part)); part.Rigidbody.collisionDetectionMode = CollisionDetectionMode.Discrete; //Don't attach if inpact force is too low if (collision.relativeVelocity.magnitude < forceNeeded) { return; } float shorterDist = Mathf.Infinity; bool nearestHitFound = false; Part nearestHitPart = null; RaycastHit nearestHit = new RaycastHit(); Vector3 rayDirection = this.part.transform.TransformDirection(rayDir); //Get all raycast hits in front of the grapple var nearestHits = new List <RaycastHit>( Physics.RaycastAll(this.part.transform.position, rayDirection, rayLenght, 557059)); foreach (RaycastHit hit in nearestHits) { //Exclude grapple collider if (hit.collider == this.part.collider) { continue; } //Exclude parts if needed if (!attachToPart && hit.rigidbody && hit.rigidbody.GetComponent <Part>()) { continue; } // Get closest hit float tmpShorterDist = Vector3.Distance(this.part.transform.position, hit.point); if (tmpShorterDist <= shorterDist) { shorterDist = tmpShorterDist; nearestHit = hit; if (nearestHit.rigidbody) { nearestHitPart = nearestHit.rigidbody.GetComponent <Part>(); } nearestHitFound = true; } } if (!nearestHitFound) { KAS_Shared.DebugLog("AttachOnCollision - Nothing to attach in front of grapple"); return; } KASModuleWinch connectedWinch = KAS_Shared.GetConnectedWinch(this.part); if (connectedWinch) { MoveAbove(nearestHit.point, nearestHit.normal, aboveDist); connectedWinch.cableJointLength = connectedWinch.cableRealLenght; } if (nearestHitPart) { KAS_Shared.DebugLog("AttachOnCollision - grappleAttachOnPart=true"); KAS_Shared.DebugLog("AttachOnCollision - Attaching to part : " + nearestHitPart.partInfo.title); AttachPartGrapple(nearestHitPart); } else { KAS_Shared.DebugLog("AttachOnCollision - Attaching to static : " + nearestHit.collider.name); AttachStaticGrapple(); } }
private IEnumerator GrabCoroutine(Vessel kerbalEvaVessel) { KAS_Shared.DebugLog("Grab - Grabbing part :" + this.part.partInfo.name); //Send message to other modules base.SendMessage("OnPartGrab", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver); //Set attach node on EVA SetEvaNode(kerbalEvaVessel); //Drop grabbed eva part if any KASModuleGrab grabbbedPartModule = KAS_Shared.GetGrabbedPartModule(kerbalEvaVessel); if (grabbbedPartModule) { grabbbedPartModule.Drop(); } //Unplug winch connected head if any KASModuleWinch moduleWinch = KAS_Shared.GetConnectedWinch(this.part); if (moduleWinch) { moduleWinch.UnplugHead(false); } //Disable all colliders on part DisableColliders(); //Detach if needed Detach(); //Decouple part (if not already done, in case of loading after a save with the part grabbed) if (this.part.vessel != kerbalEvaVessel) { KAS_Shared.DecoupleFromAll(this.part); } //Wait decouple action (x64 fix) yield return(new WaitForFixedUpdate()); //Move part to eva node KAS_Shared.MoveAlign(this.part.transform, partNode.nodeTransform, evaNodeTransform); //Grab winch connected head if any if (moduleWinch) { KASModulePort modulePort = this.part.GetComponent <KASModulePort>(); moduleWinch.GrabHead(kerbalEvaVessel, modulePort); } //Couple part to eva (if not already done, in case of loading after a save with the part grabbed) if (this.part.vessel != kerbalEvaVessel) { this.part.Couple(kerbalEvaVessel.rootPart); } //Destroy joint to avoid buggy eva move if (this.part.attachJoint) { this.part.attachJoint.DestroyJoint(); } //Set part to physic join or kinematic with parent if (physicJoint) { if (evaJoint) { Destroy(evaJoint); } evaJoint = this.part.gameObject.AddComponent <FixedJoint>(); evaJoint.connectedBody = evaCollider.attachedRigidbody; evaJoint.breakForce = 5; evaJoint.breakTorque = 5; KAS_Shared.ResetCollisionEnhancer(this.part); } else { syncGrab = true; KAS_Shared.ResetCollisionEnhancer(this.part, false); } //Add grabbed part mass to eva if (addPartMass && !physicJoint) { orgKerbalMass = kerbalEvaVessel.rootPart.mass; kerbalEvaVessel.rootPart.mass += this.part.mass; } //Add event GameEvents.onCrewBoardVessel.Add(new EventData <GameEvents.FromToAction <Part, Part> > .OnEvent(this.OnCrewBoardVessel)); //Set variables evaHolderVesselName = kerbalEvaVessel.vesselName; evaHolderPart = kerbalEvaVessel.rootPart; grabbed = true; grab_pending = false; //Refresh Context Menu RefreshContextMenu(); //Play grab sound fxSndGrab.audio.Play(); //Send message to other modules base.SendMessage("OnPartGrabbed", kerbalEvaVessel, SendMessageOptions.DontRequireReceiver); }