Lifting an object on the camera

According to the tutorial video, I created the code for lifting an object.
How do I make the object follow the camera as fast as the camera rotates? Or, at least, when the camera rotates fast, the object doesn’t fly away, and a new object doesn’t cling when there is already a lifted object.
(Pardon my English if you don’t understand)

public int GRABI;
    public float grabPower = 10.0f;
    public float throwPower = 10f;   //speed push
    public float RayDistance = 30.0f;   //distanse

    private bool Grab = false;   //attraction function
    private bool Throw = false;   //push function
    public Transform offset;
    public GameObject cam;
    RaycastHit hit;   //ray
    public float x;


    private void Start()
    {
        GRABI = 0;
         
    }
    void Update()
    {

        Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, RayDistance);
        if (Input.GetKeyDown(KeyCode.E))
        {

            if (hit.rigidbody)
            {
                GRABI = GRABI + 1;
                switch (GRABI)
                {
                    case 1:
                        Grab = true;
                        break;
                    case 2:
                        Grab = false;
                        break;
                    default:
                        break;
                }
                if (GRABI == 3)
                {
                    GRABI = 0;
                }
                if (Grab == false)
                {
                    GRABI = 0;
                }
            }
            Debug.Log(GRABI);
        }
        if (Input.GetMouseButtonDown(0))
        {
            if (Grab)
            {
                GRABI = 0;
            }
        }
        if (Input.GetMouseButtonDown(0))
        {//if the left mouse button is pressed
            if (Grab)
            {
                Grab = false;
                Throw = true;
            }
        }
            if (Grab)
        {//attraction function
            if (hit.rigidbody)
            {
                hit.rigidbody.velocity = (offset.position - (hit.transform.position + hit.rigidbody.centerOfMass)) * grabPower * x;
            }
        }
        if (Throw)
        {//push function
            if (hit.rigidbody)
            {
                hit.rigidbody.velocity = new Ray(cam.transform.position, cam.transform.forward).direction * throwPower;
                Throw = false;
            }
        }             
    }

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

You may edit your post above.

Here’s how to debug what you have so far:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Thank you, I already know the reason. The thing is that the camera is moving faster than the object behind the camera, because of this GRABY remains 1, the object that was behind the camera flew away from her, and it turns out that when the camera beam collides with another object, it rises without pressing E (do not know how to fix these two errors) I know how to fix 1, but it also appears a new error (when you pick up an object, it moves too quickly on the player and the player flew away), and also lost smoothness when taking the object. I also remembered one bug that is associated with 2, if you’re holding an object and another next to it, if the object you’re holding a bit away from the camera beam, it will take the next lying object.