void TryGrabObject() { RaycastHit hit; Ray ray = playerCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast(ray, out hit, grabRange, grabbableLayer)) { Rigidbody rb = hit.collider.attachedRigidbody; if (rb != null && rb.mass < 50f) // Avoid grabbing too-heavy objects { heldObject = rb; heldObject.useGravity = false; heldObject.drag = 10f; isHolding = true; } } } Gravity gun script
if (Input.GetButton("Fire2")) // Right click hold: Pull object toward you { if (!isHolding) PullObject(); } void TryGrabObject() { RaycastHit hit; Ray ray =
Here’s a compact (Unity/C#) that lets you pick up, hold, and launch rigidbody objects with realistic force. It’s designed for a first-person controller and works like the Half-Life 2 gravity gun. void TryGrabObject() { RaycastHit hit