/// <summary> /// Updates all of the positions of the rope LineRenderable /// so that it is drawn accurately /// </summary> private void UpdateRopeRenderablePositions() { if (IsRopeConnected()) { HookSpriteObject.SetActive(false); // if connected and release, then let go if (!FireButton.IsButtonHeld()) { Detach(); } else { // update the renderers, objects and colliders for when the rope is connected UpdateConnectedRope(); } } else { // the rope is not connected // not casting, if they hold down the button then cast if (FireButton.IsButtonHeld() && CanFire) { // update the stuff for a rope that is being cast UpdateCastingRope(); } // reset when let go else { // reset to the disconnected state ResetRopeAndHookOnRelease(); } } }
/// <summary> /// Updates all of the positions of the rope LineRenderable /// so that it is drawn accurately /// </summary> private void UpdateRopeRenderablePositions() { if (IsRopeConnected()) { HookSpriteObject.SetActive(false); // if connected and release, then let go if (!FireButton.IsButtonHeld()) { Detach(); } else { // ensure that the line renderer is enabled when the rope is connected // and the distance joint RopeLineRenderer.enabled = true; RopeDistanceJoint.enabled = true; RopeDistanceJoint.connectedAnchor = RopeAnchorPoint.transform.position; if (!HasDoneInitialReelIn) { RopeDistanceJoint.distance -= CastInitialReelIn; if (RopeDistanceJoint.distance < MinCastDistance) { RopeDistanceJoint.distance = MinCastDistance; } HasDoneInitialReelIn = true; } // reel in the player var ropeDistance = RopeDistanceJoint.distance; // adjust the target distance based on the ClimbDescend Axis ropeDistance += Input.GetAxis(ClimbDescendAxis) * ClimbDescendSpeed * Time.deltaTime; // ensure ropeDistance is in bounds if (ropeDistance > MaxCastDistance) { ropeDistance = MaxCastDistance; } else if (ropeDistance < MinCastDistance) { ropeDistance = MinCastDistance; } // set the new rope distance RopeDistanceJoint.distance = ropeDistance; // update the first point to be the same as the player origin w/ the offset //TODO: should later expand on the player offset to compensate for any rotation of the player, if that is planned to be used // could have the player rotate to match the swinging RopeLineRenderer.SetPosition(0, transform.position + PlayerRopeDrawOffset); // set the last point to be the same as the anchor point RopeLineRenderer.SetPosition(1, RopeAnchorPoint.position); } } else { // the rope is not connected // not casting, if they hold down the button then cast if (FireButton.IsButtonHeld()) { //HookCollider.enabled = true; RopeAndHookCollider.enabled = true; HookSpriteObject.SetActive(true); // start throwing if not throwing already if (!IsCasting) { IsCasting = true; // reset the casting distance CurrentCastDistance = 0; } else { // increment the casting distance CurrentCastDistance += CastingSpeed * Time.deltaTime; // ensure it fits in the upper bound if (CurrentCastDistance > MaxCastDistance) { CurrentCastDistance = MaxCastDistance; } } // update the position of the line renderer // and the collider RopeLineRenderer.enabled = true; var directionVector = new Vector3(Mathf.Cos(AimAngle), Mathf.Sin(AimAngle), 0); var displayOrigin = transform.position + PlayerRopeDrawOffset; var displayOffset = new Vector3(CurrentCastDistance * directionVector.x, CurrentCastDistance * directionVector.y, 0); RopeLineRenderer.SetPosition(0, displayOrigin); RopeLineRenderer.SetPosition(1, transform.position + displayOffset); // get the midpoint var midPoint = Vector3.Lerp(Vector3.zero, displayOffset, 0.5f); var offset = new Vector2(midPoint.x, midPoint.y).magnitude; // set the center of the collider to the midpoint between the end of the cast RopeAndHookCollider.offset = new Vector2(0, CurrentCastDistance / 2); // size the collider to fit the cast RopeAndHookCollider.size = new Vector2(0.2f, CurrentCastDistance); // set the end of the sprite to the end of the grapple HookSpriteObject.transform.localPosition = CurrentCastDistance * directionVector; // rotate the sprite so that it looks correct HookSpriteObject.transform.rotation = Quaternion.Euler(0, 0, -90 + Mathf.Rad2Deg * AimAngle); // rotate the object that contains the casting collider transform.rotation = Quaternion.Euler(0, 0, -90 + Mathf.Rad2Deg * AimAngle); } // reset when let go else { HookSpriteObject.SetActive(false); CurrentCastDistance = 0; IsCasting = false; RopeAndHookCollider.enabled = false; RopeLineRenderer.enabled = false; } } }