private void Update() { if (!ReInput.isReady) { return; } if (!playerController.initialized) { return; } if (InputPlayer == null) { return; } cameraTransform = playerCamera.TargetTransforms[0]; Ray ray = new Ray(cameraTransform.position, cameraTransform.forward * grabRange); //Debug.DrawRay(ray.origin, ray.direction, Color.cyan); trajectoryPredictor.debugLineDuration = Time.deltaTime; LeanPool.DespawnAll(); if (holdingAnObject == false) { if (!InputPlayer.GetButtonDown(PICKUP_BUTTON)) { return; } if (!Physics.Raycast(ray, out RaycastHit hit, grabRange, pickupLayermask)) { return; } heldObject = hit.collider.transform; holdingAnObject = true; heldObjectRigidBody = heldObject.GetComponent <Rigidbody>(); heldObjectCollider = heldObject.GetComponent <Collider>(); heldObjectRigidBody.isKinematic = true; initialGrab = true; //heldObjectCollider.enabled = false; } else { Transform heldTransform = heldObject.transform; Matrix4x4 matrix = Math.LocalMatrix(cameraTransform); Vector3 holdingPosition = matrix.MultiplyPoint3x4(holdOffset); //heldTransform.position = cameraTransform.TransformPoint(holdOffset); Vector3.Distance(heldTransform.position, holdingPosition); heldTransform.PositionTo(holdingPosition, 0.3f, EaseType.ExponentialIn); heldTransform.SetParent(cameraTransform); rotationInput = new Vector3( InputPlayer.GetAxis(ROTATE_HORIZONTAL), InputPlayer.GetAxis(ROTATE_VERTICAL), 0); heldTransform.Rotate(heldTransform.up, rotationInput.x); heldTransform.Rotate(heldTransform.right, rotationInput.y); //Debug.Log($"rotationInput = {rotationInput}"); //heldObjectMoveTowardsPosition = matrix.MultiplyPoint(holdOffset); if (InputPlayer.GetButtonDown(PICKUP_BUTTON)) { initialGrab = false; } if (initialGrab) { return; } if (InputPlayer.GetButton(PICKUP_BUTTON)) { timeSinceStartedCharging += Time.deltaTime; chargePercentage = timeSinceStartedCharging / chargeTime; throwForce = Mathf.Lerp(minThrowForce, maxThrowForce, chargePercentage); trajectory = trajectoryPredictor.GetTrajectoryPoints ( startPos: heldTransform.position, velocity: projectileDirection * throwForce, gravity: Physics.gravity, physicMaterial: defaultPhysicsMaterial //physicMaterial: heldObjectCollider.material ); } foreach (Vector3 orbPos in trajectory) { if (trajectoryDebugObject) { LeanPool.Spawn(trajectoryDebugObject, orbPos, Quaternion.identity); } } if (!(chargePercentage >= 0.05)) { return; } if (!InputPlayer.GetButtonUp(PICKUP_BUTTON)) { return; } heldTransform.SetParent(null); //LeanPool.DespawnAll(); timeSinceStartedCharging = 0f; heldObjectRigidBody.isKinematic = false; //heldObjectCollider.enabled = true; heldObjectRigidBody.AddForce(throwForce * projectileDirection, throwForceMode); holdingAnObject = false; heldObject = null; heldObjectRigidBody = null; //heldObjectCollider = null; } }
// This will despawn all pool clones public void DespawnAll() { LeanPool.DespawnAll(); }