void Update() { if (Input.GetButtonDown("Fire1")) // Left click: Grab/Throw { if (isHolding) ThrowObject(); else TryGrabObject(); }

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; } } }

using UnityEngine; public class GravityGun : MonoBehaviour { [Header("References")] public Camera playerCamera; public Transform holdPoint; public LayerMask grabbableLayer;

if (isHolding && heldObject != null) { // Move held object to hold point Vector3 targetPos = holdPoint.position; heldObject.velocity = (targetPos - heldObject.position) * pullForce;

// Rotate object with mouse (optional) float mouseX = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime; float mouseY = Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime; heldObject.transform.Rotate(Vector3.up, mouseX, Space.World); heldObject.transform.Rotate(Vector3.right, mouseY, Space.World); } }

leyfacturaelectronica.com
Resumen de privacidad

Esta web utiliza cookies para que podamos ofrecerte la mejor experiencia de usuario posible. La información de las cookies se almacena en tu navegador y realiza funciones tales como reconocerte cuando vuelves a nuestra web o ayudar a nuestro equipo a comprender qué secciones de la web encuentras más interesantes y útiles.

Más info sobre la política de privacidad: Política de privacidad