- Home /
Question by
hamoo7dfuad · Feb 08 at 09:21 AM ·
c#multiplayerarraysgrabplayers
how to fix the script for a multiplayer game
Hi, I have made a grab and throw script and it works perfectly as a single player but when I was trying it in multiplayer I had some bugs like 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 and when the player tries to grab the object from another player both equal to true, I'm a beginner and I will be appreciated if you help me.
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);
}
}
}
Comment
Your answer
