I’m having trouble making a retracting grapple hook similar to the Hookshot featured in most Legend of Zelda games, where the hook retracts back to the Hookshot either if the player hits a button before the hook gets to its intended destination or if the hook reaches a specified distance without hitting a grapple point.
Right now, the hook I’ve created shoots out to a specified distance away from the player, then immediately ‘snaps’ into place when it’s greater than that distance. Below is the FixedUpdate function where most of the action takes place:
void FixedUpdate()
{
hookDist = Vector3.Distance(subItem.transform.position, hook.transform.position);
Ray ray = Camera.mainCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
RaycastHit hit;
rotSpeed = Mathf.Clamp(rotSpeed, 0f, 1f);
step = speed * Time.deltaTime;
hook.transform.Rotate(Vector3.forward * (angle * rotSpeed), Space.Self);
Debug.Log(rotSpeed);
Debug.Log(hookDist);
if (Input.GetMouseButton(0))
{
if (Physics.Raycast(ray, out hit))
{
GrappleToGrapplePoint();
}
}
if (isSpinning) {
rotSpeed += .2f * Time.deltaTime;
}
else
rotSpeed -= .5f * Time.deltaTime;
if (Input.GetMouseButtonDown(0)) {
isSpinning = true;
}
if (Input.GetMouseButtonUp(0)) {
isSpinning = false;
if (rotSpeed >= 1f) {
hook.rigidbody.velocity = subItem.transform.TransformDirection(new Vector3(0, 0, hookSpeed));
hook.transform.parent = null;
}
}
if (hookDist >= 18)
{
hook.rigidbody.velocity = Vector3.zero;
hook.transform.position = Vector3.Lerp(hook.transform.position, subItem.transform.position, retractSpeed * Time.time);
hook.transform.parent = subItem.transform;
}
}
If you read the script, you can see that I use Vector3.Lerp to retract the hook to its initial position without snapping into place. However, as stated before, the hook ‘snaps’ back to the empty GameObject’s position.
As per usual, I may be missing something very small; I just can’t figure out what it is at the time. Any relevant input would be much appreciated.
Thank you for taking the time to read this.