# Player killing other player by throwing something Problem

I coded a throwing mechanic and i want it so if the first player throws the ball at the other player the 2nd player dies. Same if 2nd player is throwing at 1st.

But when the first player picked up the ball he would die.

I need a solution that makes it so the ball can kill the other player but not the player holding the ball.

Here is my Ball Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

``````public class Ball : MonoBehaviour
{

int playerStart;
[SerializeField] Player1 player1;
[SerializeField] Player2 player2;

void Start()
{
playerStart = Random.Range(0, 2);

}

// Update is called once per frame
void Update()
{
if(playerStart == 0)
{
transform.position = new Vector2(-2, 0);
playerStart = 3;
}
else if(playerStart == 1)
{
transform.position = new Vector2(2, 0);
playerStart = 3;
}

}

private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player1")
{
Destroy(player1);
}

if (collision.gameObject.tag == "Player2")
{
Destroy(player2);
}
}
}
``````

My Player Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

``````public class Player1 : MonoBehaviour
{
float deltaX;
float deltaY;
float newPosX;
float newPosY;
[SerializeField] float speed;

float xMin;
float xMax;
float yMin;
float yMax;

void Start()
{
MoveBoundaries();
}

void Update()
{
deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
deltaY = Input.GetAxis("Vertical") * Time.deltaTime * speed;
newPosX = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
newPosY = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);
transform.position = new Vector2(newPosX, newPosY);
}

void MoveBoundaries()
{
Camera gameCamera = Camera.main;
xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + paddingXMin;
xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - paddingXMax;
yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + paddingY;
yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - paddingY;
}

}
``````

And my Grab Collision Field Script if u need it:
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;
[SerializeField] bool stickingToHand;

void Start()
{

grabbing = false;
throwing = false;
}

void Update()
{

if (Input.GetKeyDown(KeyCode.G))
{
if (grabbing == true)
{
grabbing = false;
ball.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
throwing = false;
throwing = true;
stickingToHand = true;

ball.transform.position = Hands.transform.position;
ball.transform.parent = Hands.transform;

}
}

if (Input.GetKeyDown(KeyCode.T))
{
if (throwing == true)
{
grabbing = false;
stickingToHand = false;
ball.transform.parent = null;
ball.GetComponent<Rigidbody2D>().velocity = new Vector2(powerX, powerY);
throwing = false;

}
}

}

private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Ball")
{

StartCoroutine(WaitToGrab());

}

}

IEnumerator WaitToGrab()
{
grabbing = true;

yield return new WaitForSeconds(0.25f);

grabbing = false;
}

}
``````

Thanks.

In your ball script, just add a bool value to detect if the player can pick it up or not before destroying the player.