Hello,
I’ve been having some problems trying to conceptualize how to code a mechanic I’m working on. The goal is for the player to press a button which then makes the character reach out to grab an object a specified distance in front of him. If no object is detected nothing happens. If an object IS detected it’s grabbed.
Once grabbed I want the players movement to freeze, and look for directional input for “X” frames. Once input is received (lets say down arrow or down on joystick) I want the player character to yeet the object in that direction while propelling the player in the opposite direction. Movement is enabled again at some point after the player and object are launched.
As of now I can make the player press a button, send out a raycast x distance away, grab the object and freeze player movement. However I’m struggling to think of how to code the rest. At this point I’m looking for a way to look for player directional input for x frames. What should I do here? Not asking for someone to do it for me (unless you’d like to of course) just looking for some breadcrumbs. I appreciate any insights!
Function attached below. Else if statement as of now just drops the object if one is held.
public void OnWhipPerformed(InputAction.CallbackContext context)
{
if (context.performed)
{
//added to turn on whipping parameter in AnimationController
AnimationHandler.whip = true;
RaycastHit2D hitInfo = Physics2D.Raycast(rayPoint.position, transform.right, rayDistance);
if (hitInfo.collider != null && hitInfo.collider.gameObject.layer == layerIndex)
{
//grab object
if (context.performed && grabbedObject == null)
{
grabbedObject = hitInfo.collider.gameObject;
grabbedObject.GetComponent<Rigidbody2D>().isKinematic = true;
grabbedObject.transform.position = grabPoint.position;
grabbedObject.transform.SetParent(transform);
//freezes Player movement when object is held
RB.constraints = RigidbodyConstraints2D.FreezeAll;
//releases object
else if (context.performed)
{
//added to turn off whipping parameter in AnimationController
AnimationHandler.whip = false;
grabbedObject.GetComponent<Rigidbody2D>().isKinematic = false;
grabbedObject.transform.SetParent(null);
grabbedObject = null;
}
}
Debug.DrawRay(rayPoint.position, transform.right * rayDistance);
}
}