This script enables a player to pick up, hold, rotate, and throw objects within a certain range using Unity. Here's a simplified explanation of how it works:
Setup Variables: The script sets up variables such as the player GameObject, the position where objects will be held (holdPos), throw force (throwForce), and the range within which objects can be picked up (pickUpRange).
Raycasting for Object Pickup: When the player presses the designated key (default: "E"), a raycast is performed from the player's position forward to check for objects within the pickup range. If a valid object tagged as "canPickUp" is found, it's picked up.
Picking Up Objects: If the raycast hits a valid object, the PickUpObject() function is called. This function makes the object kinematic (so it doesn't respond to physics), parents it to the hold position, and ignores collisions with the player to prevent strange behavior.
Holding and Moving Objects: While holding an object, it stays positioned at holdPos. If the player presses and holds the designated rotation key (default: "R"), they can rotate the object using the mouse.
Dropping Objects: Pressing the left mouse button (default) releases the object. It restores its physics properties, stops ignoring collisions with the player, and is no longer parented to the hold position.
Throwing Objects: When the player drops the object, it can be thrown by applying a force in the direction the player is facing.
Preventing Clipping: When dropping or throwing objects, a function called StopClipping() is invoked. It checks for any objects between the player and the held object to prevent clipping, adjusting the object's position if necessary.
Overall, this script provides a basic interaction system for picking up, holding, rotating, and throwing objects in a Unity environment.