ARTICLE AD BOX
I have a PickUpObject script (which, coincidentally is from Stack Overflow) where you pick up objects.
I've realised that the object, when held, does not follow relatively to myHands; //also, you can throw the object indefinetely.
For more information:
-When holding object, Object doesnt update its position to myHands.transform.position;
-Also, when clicking F, you can keep on pressing it and it will just keep going forward.
I am a beginner coder, btw
Is there any fixes of this script?
using TMPro; using Unity.VisualScripting; using UnityEngine; using static UnityEngine.GraphicsBuffer; public class PickUpObject : MonoBehaviour { [Header("Pickup")] public Transform hand; public float pickupDistance = 3f; public LayerMask pickupMask; [Header("UI")] //public TextMeshProUGUI pickupText; [Header("Throw")] public float throwForce = 8f; // Throw force public ForceMode throwForceMode = ForceMode.VelocityChange; // Add force immediately GameObject heldObject; Rigidbody heldRb; bool isHolding; void Start() { /*if (pickupText != null) pickupText.gameObject.SetActive(false);*/ } void Update() { /*if (InputManager.Paused) return;*/ // If holding something if (isHolding) { if (!Input.GetKey(KeyCode.E)) Drop(); if (Input.GetKey(KeyCode.F)) Throw(); /*if (pickupText != null) pickupText.gameObject.SetActive(false);*/ return; } /*if (gameObject.transform.position.y <= -10) Destroy(gameObject);*/ TryPickupRaycast(); } void TryPickupRaycast() { RaycastHit hit; if (Physics.Raycast( Camera.main.transform.position, Camera.main.transform.forward, out hit, pickupDistance, pickupMask)) { Rigidbody rb = hit.collider.GetComponent<Rigidbody>(); if (rb != null) { /*if (pickupText != null) { pickupText.gameObject.SetActive(true); pickupText.text = "Press E to pick up"; }*/ if (Input.GetKey(KeyCode.E)) { PickUp(hit.collider.gameObject, rb); } } else { /*if (pickupText != null) pickupText.gameObject.SetActive(false);*/ } } else { /*if (pickupText != null) pickupText.gameObject.SetActive(false);*/ } } void PickUp(GameObject obj, Rigidbody rb) { heldObject = obj; heldRb = rb; isHolding = true; heldRb.isKinematic = true; Collider col = obj.GetComponent<Collider>(); if (col) col.enabled = false; obj.transform.SetParent(hand); obj.transform.localPosition = Vector3.zero; obj.transform.localRotation = Quaternion.identity; /*if (pickupText != null) pickupText.gameObject.SetActive(false);*/ } public void Drop() { if (!isHolding) return; isHolding = false; heldRb.isKinematic = false; Collider col = heldObject.GetComponent<Collider>(); if (col) col.enabled = true; heldObject.transform.SetParent(null); heldObject = null; heldRb = null; } void Throw() { // Identical as Drop, but addforce if (!isHolding) return; // Unparent heldObject.transform.SetParent(null); // Reestablish the New Rigidbody Targetting if (heldObject != null) heldObject.SetActive(true); if (heldRb != null) { heldRb.isKinematic = false; //?? Throw force: at myHands.position.forward Vector3 dir = hand.forward; // restart speed and then addforce heldRb.linearVelocity = Vector3.zero; heldRb.angularVelocity = Vector3.zero; heldRb.AddForce(dir * throwForce, throwForceMode); } // Reset targets //heldObject = false; //heldRb = null; //isHolding = null; } // Used by SceneTransform public bool IsHolding() => isHolding; public GameObject GetHeldObject() => heldObject; }