// SocketAnimation //[Header("Socket Animation")] //public MeshRenderer vSocketMeshRender; //public Scr_Socket_Animated vTempAnimator; void Reset() { vGrabbablePart = this.GetComponent <Scr_Grabbable>(); gGC = GameObject.FindGameObjectWithTag("GameController").GetComponent <Scr_GameController>(); cSM = this.transform.root.GetComponentInChildren <Scr_Socket_Male>(); this.enabled = false; }
public void Grip() { vTargetItem = null; //Debug.Log("Gripping"); #region Find a grabbable target if (vGrabbedItem == null) { RaycastHit tHit; Ray tRay = new Ray(vOrigin.position, (vDirection.position - vOrigin.position).normalized); Debug.DrawRay(vOrigin.position, (vDirection.position - vOrigin.position).normalized); if (Physics.SphereCast(tRay, vRadius, out tHit, vMaxDistance, vLayerMask)) { vTestSphere.transform.position = tHit.point; //Debug.Log("HIT"); if (tHit.collider.tag == "Grabbable") { vTargetItem = tHit.collider.gameObject; } } } #endregion if (vPressValue > vGrippingThreshold) { #region Start gripping if (vTargetItem != null) { if (vGrabbedItem == null) { // Get main Scr_Connect_System Part first cScrGrabbable = vTargetItem.GetComponent <Scr_Grabbable>(); if (cScrGrabbable != null) { if (cScrGrabbable.cSS != null) { cScrGrabbable.cSS.enabled = false; } } // Detach ConnectionSystem if exists if (cScrGrabbable.cCS != null) { // If grabbing gun parts, do gunpart stuff cScrGrabbable.cCS.DetachSelf(); // Reset parenting cScrGrabbable.cCS.transform.SetParent(vAnchorPart.transform); // Set new parent to follow hand vGrabbedItem = cScrGrabbable.cCS.gameObject; // Update What items is being grabbed vPositionA = cScrGrabbable.cCS.transform.position; // Setup Lerping Point } else { // If grabbing NON grabbable gunparts vGrabbedItem = cScrGrabbable.gameObject; vPositionA = cScrGrabbable.transform.position; // Setup Lerping Point } // Reset Rigidbody for holding the item cScrGrabbable.cRB.isKinematic = true; // Setup Lerping from point A to hand vIsLerping = true; vLerp = 0f; // Turn off Hand render vHandMesh.enabled = false; } } #endregion } else { #region Let go of grabbed item if (vGrabbedItem != null) { //Transform tTopParent = cPS.transform.root; //cPS = vGrabbedItem.GetComponent<Scr_Grabbable>(); //vGrabbedItem.transform.parent = null; // If grabbing gun parts, do gunpart stuff if (cScrGrabbable.cCS != null) { cScrGrabbable.cCS.DetachSelf(); // Reset parenting } // Reset rigidbody cScrGrabbable.cRB.isKinematic = false; cScrGrabbable.cRB.useGravity = true; // Multiply movement for throwing Vector3 tVelocity = (this.transform.position - vPreviousPosition) * 2500f * Time.deltaTime; cScrGrabbable.cRB.velocity = tVelocity; cScrGrabbable.cRB.angularVelocity = tVelocity; // if the grabbable has a socket, activate the auto connect if (cScrGrabbable.cSS != null) { cScrGrabbable.cSS.enabled = true; cScrGrabbable.cSS.TurnOn(); } // Reset Hand to grab again vGrabbedItem = null; cScrGrabbable = null; vHandMesh.enabled = true; } #endregion } //Debug.Log("MiddlePressUpdate " + vPressValue.ToString() + " Press / vGrippingTreshhold " + vGrippingThreshold.ToString()); vPreviousPosition = this.transform.position; vPreviousAngle = this.transform.eulerAngles; }
private void Reset() { vGameController = GameObject.FindGameObjectWithTag("GameController").transform; vMainParts = this.GetComponentsInChildren <Scr_PartParenting>(); cGrabbablePart = this.GetComponentInChildren <Scr_Grabbable>(); Scr_PartParenting tTempPP; MeshCollider tTempMC; Scr_Socket_Male tTempSM; Scr_Socket_System tTempSS; tTempSS = this.GetComponentInChildren <Scr_Socket_System>(); // Mainbody setup insurance vMainBody = this.GetComponentInChildren <MeshRenderer>().transform; if (vMainBody.transform.IsChildOf(this.transform)) { // Check if there is a part parenting, if not fully setup main part tTempPP = vMainBody.GetComponent <Scr_PartParenting>(); if (tTempPP == null) { vMainBody.tag = "Grabbable"; tTempMC = vMainBody.GetComponent <MeshCollider>(); if (tTempMC == null) { tTempMC = vMainBody.gameObject.AddComponent <MeshCollider>(); } tTempMC.convex = true; } // Add grabbable system if (vMainBody.GetComponent <Scr_Grabbable>() == null) { cGrabbablePart = vMainBody.gameObject.AddComponent <Scr_Grabbable>(); } // Part parenting system, which is commonly used for connect system with sockets if (vMainBody.GetComponent <Scr_PartParenting>() == null) { vMainBody.gameObject.AddComponent <Scr_PartParenting>(); } // if there is a socket male, setup main body to have the connect system tTempSM = this.GetComponentInChildren <Scr_Socket_Male>(); if (tTempSM != null) { if (vMainBody.GetComponent <Scr_Socket_System>() == null) { cGrabbablePart.cSS = vMainBody.gameObject.AddComponent <Scr_Socket_System>(); } } cGrabbablePart.Reset(); // //vMainBody } else { vMainBody = null; Debug.Log("The first child of this object is NOT the main body"); } // Scr_Socket_Male tTemp = null; tTemp = this.GetComponentInChildren <Scr_Socket_Male>(); //Debug.Log("vPositionOffset " + vPositionOffset.ToString()); //Debug.Log("vPositionOffset *-1 " + vPositionOffset.ToString()); ResetAllPartParenting(); if (tTempSS != null) { vAngleOffset = tTempSS.cSM.transform.localEulerAngles; } if (tTemp != null) { vPositionOffset = tTemp.transform.localPosition; } vPositionOffset.x *= Mathf.Sign(vPositionOffset.x); vPositionOffset.y *= Mathf.Sign(vPositionOffset.y); vPositionOffset.z *= Mathf.Sign(vPositionOffset.z); vAngleOffset.y += -180; ResetAllPartParenting(); }