So i want to make my button can hold or do stuff when hold, in this case I want to make grab function, when the button down can grab but if button up its not grab.
before that I already have the function and using Standart Asset
with crossplatform
.
here the preview
that’s the current button
and here the script :
void Update()
{
Physics2D.queriesStartInColliders = false;
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x, distance, boxMask);
if (PushKey())
{
isGrab = !isGrab;
if (hit.collider != null && hit.collider.gameObject.tag == "Box" && isGrab)
{
box = hit.collider.gameObject;
box.GetComponent<FixedJoint2D>().connectedBody = this.GetComponent<Rigidbody2D>();
box.GetComponent<FixedJoint2D>().enabled = true;
box.GetComponent<Rigidbody2D>().gravityScale = 1;
box.GetComponent<Rigidbody2D>().mass = 1;
}
else if (!isGrab)
{
box.GetComponent<FixedJoint2D>().enabled = false;
box.GetComponent<Rigidbody2D>().gravityScale = 6;
box.GetComponent<Rigidbody2D>().mass = 6;
}
}
}
public bool PushKey()
{
return CrossPlatformInputManager.GetButtonDown("E");
}
the current function mean first tap button grab activated
and second tap button grab deactivated
.