I want to make my button do stuff when the player holds it. In this case, I want to make a grab function: when the button is down the character can grab objects, but if the button is up they do not grab it.
I already have the function using the Standard Assets CrossPlatformInput
.
Here is how I have configured my button so far:
Here is my 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");
}
With this function, the first tap on the button means "grab activated" and the second tap means "grab deactivated", like a toggle. Instead, I want the grab to be active as long as the button is held.
isGrab = !isGrab;
on update with just the keypress state as check condition. \$\endgroup\$