- Home /
How to access the Boolean for one object?
Hi, I have a normal grab and throw script when the player grabs the ball the boolean switch to true same as the animation the script works perfectly but I have multiple players and when two or more players get to the ball and grab it at the same time all of the players isGrabed boolean and the animation switch to true but I wanted is just to make the boolean equal to true for only one object, in other words, I want the boolean equal to true for only one object.
public class test : MonoBehaviour
{
public float distance = 0f;
public Transform holdpoint;
public Transform disPos;
public LayerMask grabbable;
[HideInInspector]
public Transform currentlyGrabbedObject;
public bool isGrabed;
public float throwForce;
public Animator anim;
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
var throwing = new Vector2(transform.localScale.x, 2) * throwForce;
if (!currentlyGrabbedObject)
{
Collider2D hit = Physics2D.OverlapCircle(disPos.position, distance, grabbable);
if (hit)
{
currentlyGrabbedObject = hit.transform;
isGrabed = true;
}
}
else
{
currentlyGrabbedObject.gameObject.GetComponent<Rigidbody2D>().velocity = throwing;
currentlyGrabbedObject = null;
isGrabed = false;
}
}
if (currentlyGrabbedObject)
{
currentlyGrabbedObject.position = holdpoint.position;
}
if (isGrabed == true)
{
anim.SetBool("isGrabed", true);
}
else
{
anim.SetBool("isGrabed", false);
}
}
}
Answer by Caeser_21 · Feb 04 at 11:34 AM
You can use Physics2D.ignorecollision. First make a GameObject array of all the players in the game, then when one player grabs the ball make the ball ignore collisions for all the objects in the GameObject array. if you want the code i can send it to you.... this should work because you are working with Collider2D
Your answer
Follow this Question
Related Questions
How to assign variables to each GameObject in an array 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can't Set Animator Boolean Parameter C# 1 Answer
Keep boolean true whilst playing animation, then false 1 Answer