Make Player Hold Something

So i am making a dodge ball game and I want a way so if the player presses G while the ball is in his trigger field the ball goes close to him and travels with him.

The script is on the trigger field that i was talking about.

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

public class GrabZone : MonoBehaviour
{

    [SerializeField] Transform ball;
    [SerializeField] Player1 player;
    [SerializeField] Vector2 ballPlayerVector;
    bool grabbing;

    void Start()
    {
        ballPlayerVector = ball.transform.position - player.transform.position;
        grabbing = false;
    }

   
    void Update()
    {
        Grab();
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Ball")
        {
            if(Input.GetKeyDown(KeyCode.G))
            {
                grabbing = true;
            }
        }
    }

    void Grab()
    {
        if(grabbing == true)
        {
            Vector2 playerPos = new Vector2(player.transform.position.x, player.transform.position.y);
            ball.transform.position = playerPos + ballPlayerVector;
        }
    }

   
}

Well, I’m guessing your ‘Player’ has hands, the way I would go about this is what you’ve done with in your OnTriggerEnter2d function except I’d just add if the ball is in the player’s half and if the objects tag is Ball, grabbing = true. Then in the update I would say if grabbing is true, and player clicks key, change the position of the ball to the players hand. Something like this should work

using UnityEngine;
public class GrabZone : MonoBehaviour
{
    [SerializeField] Transform ball;
    [SerializeField] Player1 player;
    [SerializeField] Vector2 ballPlayerVector;
    [SerializeField] GameObject Hands;
    bool grabbing;
 
    void Update()
    {
        if(grabbing == true)
        {
              if(Input.GetKeyDown(KeyCode.G)
               {
                    Ball.transform.position = Hands.transform.position; // Setting ball in Player's hand
                    Ball.transform.parent = Hands.transform; // Makes the ball a child of the Player
               }
        }
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Ball")
        {
                grabbing = true;
        }
    }
}

ps: I just wrote it here, you might expect some syntax errors etc.
Hope this helps :slight_smile:

EDIT: Also one thing I didn’t add is if the ball is in the player’s half, if I get time I might do it in VS or you get the idea, don’t you???

1 Like

Hey i need your help again how do I do it so if the player is grabbing he can throw and then he stops grabbing.
Here is the code:

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

public class GrabZonePlayer1 : MonoBehaviour
{

    [SerializeField] Transform ball;
    [SerializeField] Player1 player;
    [SerializeField] Vector2 ballPlayerVector;
    [SerializeField] GameObject Hands;
    [SerializeField] float powerX;
    [SerializeField] float powerY;
    bool grabbing;
    bool throwing;

    void Start()
    {
        ballPlayerVector = ball.transform.position - player.transform.position;
        grabbing = false;
        throwing = false;
    }

   
    void Update()
    {
       
       
                if (Input.GetKeyDown(KeyCode.G))
                {
                  if (grabbing == true)
                  {
                    throwing = true;
                    ball.transform.position = Hands.transform.position;
                    ball.transform.parent = Hands.transform;
                  }
                   
                  if(throwing == true)
                  {
                    if (Input.GetKeyDown(KeyCode.T))
                    {
                       grabbing = false;
                       ball.GetComponent<Rigidbody2D>().velocity = new Vector2(powerX, powerY);

                    }
                  }

                }

           

       
       
       
           
       
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Ball")
        {
            grabbing = true;
           
        }
    }

   

   
}

Thanks in advance

Firstly, make sure you first un-child the ball from the player. Whats your exact problem with throwing the ball?

PS: Did he/she delete their account

It says “Deleted User” for me.

Yeah, just asked to make sure. Never knew you could delete your account here.