Example #1
0
    private void HandleRopedMovementFixed()
    {
        if (deroping)
        {
            Vector2 vel = (rb.position - lastPos) * (1 / Time.fixedDeltaTime);

            // Add a little go-juice if you're trying to move sideways when you let go.
            vel.x += horizontal * ropeJumpVelocity;

            rb.velocity = vel;
            deroping    = false;
        }

        Vector2[] points     = rope.GetPoints();
        int       divisions  = points.Length - 1;           // Gaps between each point that we can lerp between.
        float     scaledPos  = ropePos * divisions;         // ropePos (0 to 1) * no. of gaps scales it to (0 to points.Length -1)
        int       lowerIndex = Mathf.FloorToInt(scaledPos); // Floor to make sure we pick a valid index from the points array
        float     lerpT      = scaledPos - lowerIndex;      // Keep the scale but subtract the lower index to get our lerp t
        Vector2   playerPos;

        if (lowerIndex >= points.Length - 1)
        {
            playerPos = points[lowerIndex];
        }
        else
        {
            playerPos = Vector2.Lerp(points[lowerIndex], points[lowerIndex + 1], lerpT);
        }

        lastPos = rb.position;
        rb.MovePosition(playerPos);

        rope.AddForceAtSegment(lowerIndex, new Vector2(horizontal, 0) * ropeSwingForce);
    }