Pick Up Cube- Script Problem

I made a script that allows the player to pick up and move a cube. When the player hits the cube with a raycast, it adds a fixed joint, resets the position, bla bla bla…
My problem is that if the player is holding the cube and they press it against a wall or on any sort of collider, the cube starts to spaz out and glitch. I tried to solve this by adding a break force to the joint, but now when the joint “breaks” my cube is still floating around without gravity and is still parented to my player. How can I make the cube drop when the joint is broken? Or at least not glitch out when it is being pushed against a collider?

Heres the code for picking it up and dropping it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UseManager : MonoBehaviour
{
    public GameObject cube;
    RaycastHit hit;

    public GameObject cubeHolder;
    public Transform cubeHolderTransform;
    public FixedJoint joint;

    public bool isHolding = false;

    public void dropCube()
    {
        Destroy(cube.GetComponent<FixedJoint>());
        cube.transform.parent = null;
        cube.GetComponent<Rigidbody>().useGravity = true;
        cube.transform.localRotation = transform.rotation;
        isHolding = false;
        StartCoroutine(wait1());
        Debug.Log("Cube dropped");
    }
    IEnumerator wait1()
    {
        yield return new WaitForSeconds(0.5f);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            if (Physics.Raycast(transform.position, transform.forward, out hit, 2.5f))
            {
                //Cube pickup script
                if ((hit.transform.tag == "cube") && isHolding == false)
                {
                    cube = hit.transform.gameObject;
                    cube.transform.position = cubeHolder.transform.position;
                    cube.transform.localRotation = transform.rotation;
                    cube.transform.SetParent(cubeHolderTransform);
                    cube.GetComponent<Rigidbody>().useGravity = false;
                    joint = cube.gameObject.AddComponent<FixedJoint>();
                    joint.connectedBody = cubeHolder.GetComponent<Rigidbody>();
                    isHolding = true;
                    StartCoroutine(wait1());
                }
                else if (isHolding == true)
                {
                    dropCube();
                }
            }
        }
    }
}

Instead of creating a fixed joint you can make the cube a kinematic rigid body. It will still be able to move through walls but it won’t ‘spaz out’ when it does. There’s no need to create a joint in this case since you’re parenting the cube to the holder, so the cube will already follow the holder. To make the cube a kinematic rigid body you would write:

cube.GetComponent<Rigidbody>().isKinematic = true;

When you drop the cube just set the isKinematic attribute back to false.

Otherwise if you dont want the cube to move through walls you can keep doing what you’re doing but remove the lines:

cube.transform.SetParent(cubeHolderTransform);
cube.GetComponent<Rigidbody>().useGravity = false;

If you’re creating a fixed joint then there is no need to do this.

This worked, thank you. But pressing the cube against a wall sometimes makes it go flying. How can I fix this? Perhaps increase the drag?

Not sure, you’ll have to play around with the physics settings. Increasing drag might help, decreasing the break force on the joint. Maybe changing the rigid body interpolation mode will help too.

Pretty sure fixed joint is the only proper way to pick up objects affected by physics. You definitely do not want your picked up objects phasing through the environment.

You should try tweaking the mass, drag, maybe angular drag of the objects if you haven’t.