コード例 #1
0
    private void AttachRope()
    {
        Player.Crosshair.gameObject.SetActive(false);
        Player.MyRopeLineRenderer.gameObject.SetActive(true);
        Player.RopeHingeAnchor.gameObject.SetActive(true);

        RaycastHit2D hit = Physics2D.Raycast(PlayerPosition, AimDirection, PlayerData.ropeMaxCastDistance, PlayerData.whatCanYouAttachTo);

        if (hit.collider != null)
        {
            RopeAttached   = true;
            PlayerPosition = Player.transform.position;
            AddForceAfterRopeAttaching(hit.point);

            if (!RopePositions.Contains(hit.point))
            {
                RopePositions.Add(hit.point);

                Player.RopeJoint.distance = Vector2.Distance(PlayerPosition, hit.point);
                Player.RopeJoint.enabled  = true;
                Player.RopeHingeAnchorSpriteRenderer.enabled = true;
            }
        }
        else
        {
            RopeAttached  = false;
            IsAiming      = false;
            IsHoldingRope = false;

            Player.RopeJoint.enabled = false;
            Player.RopeHingeAnchorSpriteRenderer.enabled = false;
        }
    }
コード例 #2
0
    private void MoveOnRope()
    {
        if (_xInput != 0 && !IsGrounded)
        {
            Vector2 playerToHookDirection = (_ropeHook - PlayerPosition).normalized;

            Vector2 perpendicularDirection;
            _ropeHook = RopePositions.Last();

            if (_xInput < 0)
            {
                perpendicularDirection = new Vector2(-playerToHookDirection.y, playerToHookDirection.x);

                //Vector2 leftPerpendicularPosition = PlayerPosition - perpendicularDirection;
                //Debug.DrawLine(PlayerPosition, leftPerpendicularPosition, Color.green, 0f);
            }
            else
            {
                perpendicularDirection = new Vector2(playerToHookDirection.y, -playerToHookDirection.x);

                //Vector2 rightPerpendicularPosition = PlayerPosition + perpendicularDirection;
                //Debug.DrawLine(PlayerPosition, rightPerpendicularPosition, Color.green, 0f);
            }

            Vector2 swingforce = perpendicularDirection * PlayerData.ropeSwigForce;
            Player.AddForce(swingforce, ForceMode2D.Force);
        }
    }
コード例 #3
0
    private void WrapRopeAroundObjects()
    {
        if (RopePositions.Count == 0)
        {
            return;
        }

        Vector2      lastRopePoint          = RopePositions.Last();
        RaycastHit2D playerToCurrentNextHit = Physics2D.Raycast(PlayerPosition, (lastRopePoint - PlayerPosition).normalized,
                                                                Vector2.Distance(PlayerPosition, lastRopePoint) - 0.1f, PlayerData.whatCanYouAttachTo);

        if (playerToCurrentNextHit)
        {
            CompositeCollider2D platformsCollider = playerToCurrentNextHit.collider as CompositeCollider2D;

            if (platformsCollider != null)
            {
                Vector2 closestPointToHit = platformsCollider.bounds.ClosestPoint(playerToCurrentNextHit.point);

                if (WrapPointsLookup.ContainsKey(closestPointToHit))
                {
                    Player.OnRopeStateFinish.ResetRope();
                }
                else
                {
                    RopePositions.Add(closestPointToHit);
                    WrapPointsLookup.Add(closestPointToHit, 0);
                    _distanceSet = false;
                }
            }
        }
    }
コード例 #4
0
    private void UpdateRopePositions()
    {
        // plus one for the Player position
        Player.MyRopeLineRenderer.positionCount = RopePositions.Count + 1;

        for (int i = Player.MyRopeLineRenderer.positionCount - 1; i >= 0; i--)
        {
            if (i != Player.MyRopeLineRenderer.positionCount - 1)
            {
                Player.MyRopeLineRenderer.SetPosition(i, RopePositions[i]);

                if (i == RopePositions.Count - 1 || RopePositions.Count == 1)
                {
                    Vector2 ropePosition = RopePositions[RopePositions.Count - 1];

                    if (RopePositions.Count == 1)
                    {
                        Player.RopeHingeAnchorRigidbody.transform.position = ropePosition;

                        if (!_distanceSet)
                        {
                            Player.RopeJoint.distance = Vector2.Distance(PlayerPosition, ropePosition);
                            _distanceSet = true;
                        }
                    }
                    else
                    {
                        Player.RopeHingeAnchorRigidbody.transform.position = ropePosition;

                        if (!_distanceSet)
                        {
                            Player.RopeJoint.distance = Vector2.Distance(PlayerPosition, ropePosition);
                            _distanceSet = true;
                        }
                    }
                }
                else if (i - 1 == RopePositions.IndexOf(RopePositions.Last()))
                {
                    Vector2 ropePosition = RopePositions.Last();

                    Player.RopeHingeAnchorRigidbody.transform.position = ropePosition;

                    if (!_distanceSet)
                    {
                        Player.RopeJoint.distance = Vector2.Distance(PlayerPosition, ropePosition);
                        _distanceSet = true;
                    }
                }
            }
            else
            {
                Player.MyRopeLineRenderer.SetPosition(i, PlayerPosition);
            }
        }
    }
コード例 #5
0
    public void ResetRope()
    {
        Player.RopeJoint.enabled = false;
        Player.RopeHingeAnchorSpriteRenderer.enabled = false;

        Player.MyRopeLineRenderer.positionCount = 2;
        Player.MyRopeLineRenderer.SetPosition(0, PlayerPosition);
        Player.MyRopeLineRenderer.SetPosition(1, PlayerPosition);

        WrapPointsLookup.Clear();
        RopePositions.Clear();
    }
コード例 #6
0
    private void UnwrapRopePosition(int anchorIndex, int hingeIndex)
    {
        var newAnchorPosition = RopePositions[anchorIndex];

        WrapPointsLookup.Remove(RopePositions[hingeIndex]);
        RopePositions.RemoveAt(hingeIndex);

        Player.RopeHingeAnchorRigidbody.transform.position = newAnchorPosition;
        _distanceSet = false;

        if (_distanceSet)
        {
            return;
        }

        Player.RopeJoint.distance = Vector2.Distance(PlayerPosition, newAnchorPosition);
        _distanceSet = true;
    }