Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Trying to get distancejoint2d working for rope climbing.
  • Loading branch information
EricFreeman committed Nov 4, 2014
1 parent dac2733 commit 86950fa
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions src/Assets/Scripts/Player.cs
Expand Up @@ -26,8 +26,6 @@ private bool _isOnRope
private bool _isDead;
private int _remainingBlood = 10;

private GameObject _moveTowardsCenter;

private GameObject _dragObject;
public Vector3 _dragOffset;

Expand Down Expand Up @@ -59,6 +57,15 @@ void FixedUpdate()

if (middle != null)
{
// Stick player to rope segment
if (gameObject.GetComponent<DistanceJoint2D>() == null)
{
var distance = gameObject.AddComponent<DistanceJoint2D>();
distance.maxDistanceOnly = true;
distance.distance = .24f;
}
gameObject.GetComponent<DistanceJoint2D>().connectedBody = middle.rigidbody2D;

// Climb rope
rigidbody2D.velocity = new Vector2(middle.transform.up.x * Input.GetAxisRaw("Vertical"), Input.GetAxisRaw("Vertical") * Speed * middle.transform.up.y);

Expand All @@ -70,16 +77,6 @@ void FixedUpdate()
_prevHighest = middle;
_prevPosition = middle.transform.position;
}

// Move towards middle of rope if you've somehow moved off of it
if (_moveTowardsCenter != null)
{
transform.position = Vector3.MoveTowards(transform.position,
_moveTowardsCenter.transform.position - _moveTowardsCenter.transform.up * .24f, .1f);

if (Vector3.Distance(transform.position, _moveTowardsCenter.transform.position) < 1)
_moveTowardsCenter = null;
}
}
else
{
Expand Down Expand Up @@ -133,24 +130,19 @@ private void OnTriggerEnter2D(Collider2D col)
rigidbody2D.velocity = Vector2.zero;
_ropeSegments.Clear();
}
else if (col.tag == "Rope")
{
if (_ropeSegments.Count() == 1 && col.gameObject != _moveTowardsCenter)
{
_moveTowardsCenter = null;
_ropeSegments.Add(col.gameObject);
}
}
}

private void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "Rope")
{
if (_ropeSegments.Count > 1)
_ropeSegments.Remove(col.gameObject);
else if(_ropeSegments.Count == 1)
_moveTowardsCenter = _ropeSegments[0].gameObject;
_ropeSegments.Remove(col.gameObject);

if (!_ropeSegments.Any())
{
if (gameObject.GetComponent<DistanceJoint2D>() != null)
Destroy(gameObject.GetComponent<DistanceJoint2D>());
}
}
}

Expand Down

0 comments on commit 86950fa

Please sign in to comment.