For our first Bit we’ll be making a simple script that lets you pick up something when you go near it, and then either drop it, or fire it forwards.

The first thing we need to do is make a spot for our objects to get held.
Create an empty game object and place it in front of your player. Don’t put it too close, the object will be centred on this spot.
Make sure it’s a child of the player, so it moves around as you do. See the bottom right corner for a demonstration.

Write some code

Now, let’s make a script.
Right-click in your project (I like to use a folder just for scripts and choose Create > C# Script.
Let’s call it “BasketballToss“.
Once it’s created, double click it to open it up.

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

public class BasketballToss : MonoBehaviour
{
        // Start is called before the first frame update
    void Start()
    {
    
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

We’ve got the standard Start and Update functions.
So let’s create some variables we’ll need.

    [Header("Object References")]
    public GameObject handSpot;
    //A reference to the object being held
    GameObject heldObject;
    //A bool to easily tell if we're holding something or not. We could use this to control animations, movement speed, or even input
    bool isHolding = false;
    //A reference to the collider on the held object to easily switch it on and off
    Collider heldObjectsCollider;
    //A reference to
    Rigidbody heldObjectsRigidBody;

Let’s walk through it.
The [Header] is there to appear in the inspector. Our public variables will be visible in Unity on the Basketball toss component. We can use headers to identify and put them into groups.

We’ve got variables in there for the little handspot we created, the object being held (if there is one right now), a bool (that’s true/false) to track whether something is in our hand, and references to some components.

Components are how Unity puts things together. The mesh renderer, mesh filter, and rigidbody on a model are all components. In this case, we’re getting references to the rigidbody and the collider. A reference is like making a map that points to something.

Tip: When writing C#, Collider as a component refers to ALL colliders. So instead of looking for box collider, mesh collider, capsule collider etc. You can find whichever is there easily.

Now that we’ve got our variables, let’s make something happen. We’ll do this in the Update() function.
This runs once per frame. So if it’s running a game at 30fps, we do 30 Updates a second. 300 FPS = 300 updates per second.

void Update()
    {
        //GetKeyDown is TRUE on the exact frame the key is pressed. This means if it's held down, we only trigger the code once. GetKey is TRUE for as long as key is pressed. It will run many times. Choose which one fits your needs. You can change the letter here to change what key we're looking for
        if (Input.GetKeyDown("r"))
        {
            //We switch depending on the value of isHolding. Whichever case matches, that's the code we run.
            switch (isHolding)
            {
                //if it's TRUE, we're holding something. So let's make the key drop it.
                case true:
                    //Let's turn it's collider on
                    heldObjectsCollider.enabled = true;
                    //And make it NOT kinematic. Kinematic means dont use physics. So we wont that off
                    heldObjectsRigidBody.isKinematic = false;
                    //Then let's update the bool so we know our hand is empty
                    isHolding = false;
                    //Then we will remove the parent from the held object. If it's not parented, it won't follow us anymore
                    heldObject.transform.SetParent(null);
                    //And then we clear the variables so we don't run into any issues
                    heldObject = null;
                    heldObjectsCollider = null;
                    heldObjectsRigidBody = null;
                    //the break means the end of this case. The code will not run anything else inside the Switch statement.
                    break;

                case false:
                    //First we declare a new variable. The type is RaycastHit. This is an array of all kinds of data on what our ray hits. Our hit is called "hit"
                    RaycastHit hit;
                    //Now we declare a variable to control what our Raycast will hit
                    LayerMask mask = LayerMask.GetMask("tossable");
                    //IF wrapped around a raycast will fire the raycast, and return TRUE if it hits.
                    //This is a nice neat way to only run your code if you hit something.
                    if (Physics.Raycast(transform.position + new Vector3(0, 0.2f, 0), transform.TransformDirection(Vector3.forward), out hit, 10, mask))
                    {
                        //If we hit something, we move it into the position of our Handspot
                        hit.transform.position = handSpot.transform.position;
                        //We find the collider on the object and store it in our variable
                        heldObjectsCollider = hit.transform.gameObject.GetComponent<Collider>();
                        //and we do the same thing with the rigidbody
                        heldObjectsRigidBody = hit.transform.gameObject.GetComponent<Rigidbody>();
                        //Next we turn off the collider, so the thing we hold won't run into anything. Or us!
                        //If we run into a collider that's attached to us, things can break
                        heldObjectsCollider.enabled = false;
                        //Then we set the rigidbody to kinematic. So the object won't fall with physics anymore.
                        heldObjectsRigidBody.isKinematic = true;
                        //And we update our bool to confirm we're holding something.
                        isHolding = true;
                        //Lastly we grab a reference to the object in our hand to store for later
                        heldObject = hit.transform.gameObject;
                        //And then we attach the object to us. While leaving it where it is. Now it's "in our hand".
                        heldObject.transform.SetParent(this.transform);
            }
                    //and the break means we're done here.
                        break;

            }
        }

        //Now let's check for the mouse button. Button 0 is the left button. Button 1 is the right and buton 2 is a click on the scroll wheel.
        if (Input.GetMouseButtonDown(0))
        {
            //And we also want to check if we're holding something. No point trying to throw it if we're not holding.
            if (isHolding)
            {
                //Let's switch the collider back on. We stored these variables AS we picked it up. So as long as we're holding something, they'll be there.
                heldObjectsCollider.enabled = true;
                //And then let's turn off the kinematic, just like we did when we dropped it.
                heldObjectsRigidBody.isKinematic = false;
                //Let's update the bool to know our hand is empty
                isHolding = false;
                //And then remove the parent so this thing isn't attached to us anymore.
                heldObject.transform.SetParent(null);
                //Now we apply a force to the rigidbody on the held object. Like a kick.
                // We use this.transform.forward to get the direction we are facing. It returns a vector facing in the right direction. But the LENGTH of the vector is always 1. So we multiply by 80 to give it a little kick.
                //We do the same for the UP vector from ourselves, so we can shoot it up JUST a little for a nice curve. 
                //Declaring the force as an impulse means it all happens in one frame. Kick!
                heldObjectsRigidBody.AddForce((this.transform.forward*80)+(this.transform.up*20), ForceMode.Impulse);
                //And now that we've kicked it away, let's clear those vars so we don't accidentally use them.
                heldObject = null;
                heldObjectsCollider = null;
                heldObjectsRigidBody = null;
            }
        }
    }

The code above is well commented. But overall we’ve got two separate “loops” in our code. In our first loop we check for the R key and then split if we’re holding something or not.

  • If we’re holding something => Drop it
  • If we’re not => Check if there is something in front of us
  • If there IS something suitable in front of us => Pick it up
  • If there is nothing suitable in front of us => do nothing

Notice we said “Something suitable” when we’re talking about what to pick up. That’s controlled via the layer mask variable. By placing the objects on different layers we can easily control what we do. This means you could pick up a ball, a bowl or a rock, but not a troll or spaceship.

In our second loop, we’re looking for the left mouse button to be clicked. And we split if we’re holding something or not.

  • If there’s something in our hand => Kick it away!
  • If there’s nothing in our hand => do nothing

The only other thing we need is a little Debug to tell us if we left something out.
So let’s see the whole script.

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

public class BasketballToss : MonoBehaviour
{
    [Header("Object References")]
    public GameObject handSpot;
    //A reference to the object being held
    GameObject heldObject;
    //A bool to easily tell if we're holding something or not. We could use this to control animations, movement speed, or even input
    bool isHolding = false;
    //A reference to the collider on the held object to easily switch it on and off
    Collider heldObjectsCollider;
    //A reference to
    Rigidbody heldObjectsRigidBody;
    
    

    // Start is called before the first frame update
    void Start()
    {
        if(handSpot == null)
        {
            Debug.Log("The hand object is not set");
        }
    }

    // Update is called once per frame
    void Update()
    {
        //GetKeyDown is TRUE on the exact frame the key is pressed. This means if it's held down, we only trigger the code once. GetKey is TRUE for as long as key is pressed. It will run many times. Choose which one fits your needs. You can change the letter here to change what key we're looking for
        if (Input.GetKeyDown("r"))
        {
            //We switch depending on the value of isHolding. Whichever case matches, that's the code we run.
            switch (isHolding)
            {
                //if it's TRUE, we're holding something. So let's make the key drop it.
                case true:
                    //Let's turn it's collider on
                    heldObjectsCollider.enabled = true;
                    //And make it NOT kinematic. Kinematic means dont use physics. So we wont that off
                    heldObjectsRigidBody.isKinematic = false;
                    //Then let's update the bool so we know our hand is empty
                    isHolding = false;
                    //Then we will remove the parent from the held object. If it's not parented, it won't follow us anymore
                    heldObject.transform.SetParent(null);
                    //And then we clear the variables so we don't run into any issues
                    heldObject = null;
                    heldObjectsCollider = null;
                    heldObjectsRigidBody = null;
                    //the break means the end of this case. The code will not run anything else inside the Switch statement.
                    break;

                case false:
                    //First we declare a new variable. The type is RaycastHit. This is an array of all kinds of data on what our ray hits. Our hit is called "hit"
                    RaycastHit hit;
                    //Now we declare a variable to control what our Raycast will hit
                    LayerMask mask = LayerMask.GetMask("tossable");
                    //IF wrapped around a raycast will fire the raycast, and return TRUE if it hits.
                    //This is a nice neat way to only run your code if you hit something.
                    if (Physics.Raycast(transform.position + new Vector3(0, 0.2f, 0), transform.TransformDirection(Vector3.forward), out hit, 10, mask))
                    {
                        //If we hit something, we move it into the position of our Handspot
                        hit.transform.position = handSpot.transform.position;
                        //We find the collider on the object and store it in our variable
                        heldObjectsCollider = hit.transform.gameObject.GetComponent<Collider>();
                        //and we do the same thing with the rigidbody
                        heldObjectsRigidBody = hit.transform.gameObject.GetComponent<Rigidbody>();
                        //Next we turn off the collider, so the thing we hold won't run into anything. Or us!
                        //If we run into a collider that's attached to us, things can break
                        heldObjectsCollider.enabled = false;
                        //Then we set the rigidbody to kinematic. So the object won't fall with physics anymore.
                        heldObjectsRigidBody.isKinematic = true;
                        //And we update our bool to confirm we're holding something.
                        isHolding = true;
                        //Lastly we grab a reference to the object in our hand to store for later
                        heldObject = hit.transform.gameObject;
                        //And then we attach the object to us. While leaving it where it is. Now it's "in our hand".
                        heldObject.transform.SetParent(this.transform);
            }
                    //and the break means we're done here.
                        break;

            }
        }

        //Now let's check for the mouse button. Button 0 is the left button. Button 1 is the right and buton 2 is a click on the scroll wheel.
        if (Input.GetMouseButtonDown(0))
        {
            //And we also want to check if we're holding something. No point trying to throw it if we're not holding.
            if (isHolding)
            {
                //Let's switch the collider back on. We stored these variables AS we picked it up. So as long as we're holding something, they'll be there.
                heldObjectsCollider.enabled = true;
                //And then let's turn off the kinematic, just like we did when we dropped it.
                heldObjectsRigidBody.isKinematic = false;
                //Let's update the bool to know our hand is empty
                isHolding = false;
                //And then remove the parent so this thing isn't attached to us anymore.
                heldObject.transform.SetParent(null);
                //Now we apply a force to the rigidbody on the held object. Like a kick.
                // We use this.transform.forward to get the direction we are facing. It returns a vector facing in the right direction. But the LENGTH of the vector is always 1. So we multiply by 80 to give it a little kick.
                //We do the same for the UP vector from ourselves, so we can shoot it up JUST a little for a nice curve. 
                //Declaring the force as an impulse means it all happens in one frame. Kick!
                heldObjectsRigidBody.AddForce((this.transform.forward*80)+(this.transform.up*20), ForceMode.Impulse);
                //And now that we've kicked it away, let's clear those vars so we don't accidentally use them.
                heldObject = null;
                heldObjectsCollider = null;
                heldObjectsRigidBody = null;
            }
        }
    }
}

Set things up

Add the BasketballToss component onto your Player.

And then drag your handSpot object in to set it.

Make a new layer called “tossable” and assign it to anything you want to toss around.
And you’re all set.

Tip: The raycast to find the tossable object on line 62 assumes your player pivot point is at your feet. The ray is cast slightly up to find things on the ground in front of you.
If your pivot point is in the centre of the player vertically change the "transform.position + new Vector3(0, 0.2f, 0)" on that line to "transform.position + new Vector3(0, -0.5f, 0)" to fire downwards instead
Pick it up, drop it again, and throw it away!