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);
}
}