Throwing picked up Players/Objects

The most basic concept of my game - 2D co-op puzzle game.

The players can pick up and move/throw objects in the environment as well as pickup each other player (kind of like in LoZ: Tri-Force Heroes).

I have implemented the mechanics to have the player able to pickup and put down both objects and players, but I cant figure out how to throw the player/object properly after picking it up. The way I have it configured, the object/player just gets thrown straight up into the air instead of at a (roughly) 45 degree angle in front of the player. I want to be able to take these players and objects and throw them up onto the ledge in my game but I’m having trouble figuring out the logic.

Current Result:

private void PlayerCheck()
    {
        RaycastHit2D playerInfo = Physics2D.Raycast(playerRaycast.position, Vector2.right, rayDistance, player2Layer);

        if (playerInfo.collider != null && playerInfo.collider.gameObject.layer == layerIndex2)
        {
            if (grabbedPlayer == null && !holdingObject && Keyboard.current.eKey.wasPressedThisFrame)
            {
                //pickup player if not holding a player or object if colliding and pressing the eKey
                grabbedPlayer = playerInfo.collider.gameObject;
                grabbedPlayer.transform.position = grabPosition.position;
                grabbedPlayer.GetComponent<Rigidbody2D>().simulated = false;
                grabbedPlayer.transform.SetParent(transform);
                holdingPlayer = true;

                if (holdingPlayer)
                {
                grabbedPlayer.GetComponent<SpriteRenderer>().sortingOrder = 0;              

                }
                else
                {
                    grabbedPlayer.GetComponent<SpriteRenderer>().sortingOrder = 1;
                }

            }
        }
        else if (grabbedPlayer != null && Keyboard.current.eKey.wasPressedThisFrame)
        {
            //if a player is held and eKey pressed set the player down.
            grabbedPlayer.transform.position = dropPosition.position;
            grabbedPlayer.GetComponent<Rigidbody2D>().simulated = true;
            grabbedPlayer.transform.SetParent(null);
            grabbedPlayer = null;
            holdingPlayer = false;           
        }
        else if(grabbedPlayer != null && Keyboard.current.leftShiftKey.isPressed)
        {
            //throw Player
            grabbedPlayer.GetComponent<Rigidbody2D>().velocity = new Vector2(grabbedPlayer.transform.localScale.x, 1) * throwForce;
            grabbedPlayer.GetComponent<Rigidbody2D>().simulated = true;
            grabbedPlayer.transform.SetParent(null);       
            holdingPlayer = false;
            throwPlayer = true;
            grabbedPlayer = null;                                   
        }
       
        if (playerInfo)
        {
            Color color1 = Color.green;
            Debug.DrawRay(playerRaycast.position, Vector2.right * rayDistance, color1);
        }
        else
        {
            Color color1 = Color.red;
            Debug.DrawRay(playerRaycast.position, Vector2.right * rayDistance, color1);
        }
          
    }

That’s a lot of code to post just to ask how to set the Rigidbody2D.velocity which is just a Vector2 direction with its magnitude being the speed i.e.

grabbedPlayer.GetComponent<Rigidbody2D>().velocity = new Vector2(grabbedPlayer.transform.localScale.x, 1) * throwForce;

You’re going “up” because you create a Vector2(0, 1) * throwForce because your localScale.x has to be 0. Just debug it. Start by reducing how much you do on each line. Calculate the velocity then assign it separately. This gives you the change to use things like “Debug.Log()” easier.

Also, you have a lot of redundancy here doing things like getting the Rigidbody2D again and again which isn’t free. Get it once, use it many times.

I have set it with a direct number like 1 or 100:

grabbedPlayer.GetComponent<Rigidbody2D>().velocity = new Vector2 (1,1) *throwForce;

and it makes no difference. it still goes straight up in the air

Pretty difficult to know really. You are free to attach a debugger and put a breakpoint there and read the velocity. You can also look at the velocity which is shown in the inspector in the “Info” rollout of the Rigidbody2D.

Depends on what else you have going on for a player. Maybe you’re setting constraints in X or something somewhere. Try enabling physics simulation for that body before you set the velocity although that really shouldn’t make a difference. It can receive velocity, just not act on it when not in the simulation.

Something might be removing the X component of the velocity such as your inputs.

1 Like

Does the other guy you’re throwing also have a player script driving his position or velocity?

1 Like

update: I was able to get it all working as intended. The movement along the X axis was hindered by the player controller. T

https://www.youtube.com/watch?v=mP1vBZYxBSg

2 Likes