void Update() { if (Input.GetKeyDown(KeyCode.E) && currentPayload != null && currentPayloadCargo == null) { currentPayload.transform.SetParent(transform, true); Rigidbody2D cb = currentPayload.GetComponent <Rigidbody2D> (); //Rigidbody2D lb = GetComponentInParent<Rigidbody2D>(); crateMass = cb.mass; GetComponentInParent <Rigidbody2D> ().mass += crateMass; Destroy(cb); currentPayloadCargo = currentPayload; currentPayload = null; hasCargo = true; pickupInstrictions.gameObject.SetActive(false); return; } if (Input.GetKeyDown(KeyCode.E) && currentPayloadCargo != null) { Vector2 cratePosition = currentPayloadCargo.transform.position; currentPayloadCargo.transform.SetParent(null, true); currentPayloadCargo.transform.position = cratePosition; Rigidbody2D cb = currentPayloadCargo.gameObject.AddComponent <Rigidbody2D> (); cb.mass = crateMass; Rigidbody2D landerBody = GetComponentInParent <Rigidbody2D> (); landerBody.mass -= crateMass; //landerBody.GetRelativePointVelocity (transform.localPosition); Vector2 crateVelocity = landerBody.GetRelativePointVelocity(transform.localPosition); cb.velocity = crateVelocity; currentPayloadCargo = null; hasCargo = false; return; } }
//Throws crate if currently holding one as oppose to shooting. public void ThrowObject() { if ((Input.GetMouseButtonDown(0) || Input.GetAxis("RT") == 1) && !bJustShot) { nearbyCrate.bIsPickedUp = false; Rigidbody nearbyCrateRigidbody = nearbyCrate.GetComponent <Rigidbody>(); nearbyCrateRigidbody.isKinematic = false; //Account for which direction the player is looking. if (playerBehaviour.fFlipMove < 0) { iPlayerDirection = 1; } else { iPlayerDirection = -1; } //Accounts for whether player is upside down or on ground. if (!playerBehaviour.bIsGravityReversed) { iPlayerReversed = 1; } else { iPlayerReversed = -1; } //Applies Force in impluse mode to simulate a throw using the above variables to account for different scenarios. if (nearbyCrate.bIsObjectHeavy) { nearbyCrateRigidbody.AddForce(new Vector3(15f * iPlayerDirection, 15f * iPlayerReversed, 0f), ForceMode.Impulse); } else if (nearbyCrate.bIsObjectLight) { nearbyCrateRigidbody.AddForce(new Vector3(45f * iPlayerDirection, 40f * iPlayerReversed, 0f), ForceMode.Impulse); } else { nearbyCrateRigidbody.AddForce(new Vector3(35f * iPlayerDirection, 55f * iPlayerReversed, 0f), ForceMode.Impulse); } bHoldingCrate = false; } }