private IEnumerator Climb(ClimbingNode nextNode) { UpdateAnimator(); elapsedTime = 0; while (elapsedTime <= 1) { if (Input.GetButtonDown("Jump")) { Jump(); yield break; } //Move Right Side if (movePolarity == RIGHT) { IKController.LerpIKTarget(IK.RightHand, IKTarget.FromTransform(currentNodes[RIGHT].rightHand.transform), IKTarget.FromTransform(nextNode.rightHand.transform), elapsedTime); IKController.LerpIKTarget(IK.RightFoot, IKTarget.FromTransform(currentNodes[RIGHT].rightFoot.transform), IKTarget.FromTransform(nextNode.rightFoot.transform), elapsedTime); } else { IK.RightHand.Set(currentNodes[RIGHT].rightHand); IK.RightFoot.Set(currentNodes[RIGHT].rightFoot); } //Move Left Side if (movePolarity == LEFT) { IKController.LerpIKTarget(IK.LeftHand, IKTarget.FromTransform(currentNodes[LEFT].leftHand.transform), IKTarget.FromTransform(nextNode.leftHand.transform), elapsedTime); IKController.LerpIKTarget(IK.LeftFoot, IKTarget.FromTransform(currentNodes[LEFT].leftFoot.transform), IKTarget.FromTransform(nextNode.leftFoot.transform), elapsedTime); } else { IK.LeftHand.Set(currentNodes[LEFT].leftHand); IK.LeftFoot.Set(currentNodes[LEFT].leftFoot); } //Root Rotaion - While moving Quaternion desiredRotation = Quaternion.Lerp( Quaternion.Lerp(currentNodes[LEFT].transform.rotation, currentNodes[RIGHT].transform.rotation, 0.5f), Quaternion.Lerp(currentNodes[(movePolarity + 1) % 2].transform.rotation, nextNode.transform.rotation, 0.5f), elapsedTime ); if (freehang) { desiredRotation = Quaternion.Euler(0, desiredRotation.eulerAngles.y, desiredRotation.eulerAngles.z); } rb.transform.rotation = desiredRotation; //Root Position - While moving rb.transform.position = Vector3.Lerp( (currentNodes[LEFT].PlayerPosition + currentNodes[RIGHT].PlayerPosition) / 2, (currentNodes[(movePolarity + 1) % 2].PlayerPosition + nextNode.PlayerPosition) / 2, elapsedTime); elapsedTime += Time.deltaTime * movementSpeed; yield return(null); } currentNodes[movePolarity] = nextNode; moving = false; }
void Start() { currentNode = GetComponent <ClimbingNode>(); LayerMask mechanics = 8; mechanics = ~mechanics; NodeTriggers = Physics.OverlapSphere(transform.position, DetectionRadius, mechanics); Nodes = new ClimbingNode[NodeTriggers.Length]; for (int i = 0; i < NodeTriggers.Length; i++) { ClimbingNode checkNode = NodeTriggers[i].GetComponent <ClimbingNode>(); if (checkNode) { if (checkNode != Nodes[i]) { Nodes[i] = checkNode; } } } CompareDirection[0] = transform.up; CompareDirection[1] = (transform.up + transform.right).normalized; CompareDirection[2] = transform.right; CompareDirection[3] = (-transform.up + transform.right).normalized; CompareDirection[4] = -transform.up; CompareDirection[5] = (-transform.up - transform.right).normalized; CompareDirection[6] = -transform.right; CompareDirection[7] = (transform.up - transform.right).normalized; }
private void DetermineNeighbour(ClimbingNode checkNode) { for (int i = 0; i < CompareDirection.Length; i++) { Vector3 relativeDirection = Quaternion.AngleAxis(checkNode.transform.eulerAngles.y - transform.eulerAngles.y, transform.up) * CompareDirection[i]; float compareAngle = 22.5f + 45f * (1f - (checkNode.transform.position - transform.position).magnitude); if (Vector3.Angle(relativeDirection, checkNode.transform.position - transform.position) < compareAngle) { float newDistance = (checkNode.transform.position - transform.position).magnitude; if (neighbours[i] != null) { if (newDistance < distances[i]) { neighbours[i] = checkNode; distances[i] = newDistance; } } else { neighbours[i] = checkNode; distances[i] = newDistance; } } } }
public override void UpdateIK() { if (nextNode) { float duration = (Player.transform.position - nextNode.leftHand.position).magnitude * MovementSpeed * Time.deltaTime; IK.MoveRoot(nextNode.transform, duration, Offset); if (nextMove == RIGHT) { float handDuration = (currentRight.rightHand.position - nextNode.rightHand.position).magnitude * MovementSpeed * Time.deltaTime; float footDuration = (currentRight.rightFoot.position - nextNode.rightFoot.position).magnitude * MovementSpeed * Time.deltaTime; bool done = true; if (!(IK.MoveRightHand(nextNode.rightHand.transform, handDuration))) { done = false; } if (!(IK.MoveRightFoot(nextNode.rightFoot.transform, footDuration))) { done = false; } if (done) { currentRight = nextNode; currentNode = nextNode; lastMove = RIGHT; lastIndex = nodeIndex; moving = false; } } else if (nextMove == LEFT) { float handDuration = (currentLeft.leftHand.position - nextNode.leftHand.position).magnitude * MovementSpeed * Time.deltaTime; float footDuration = (currentLeft.leftFoot.position - nextNode.leftFoot.position).magnitude * MovementSpeed * Time.deltaTime; bool done = true; if (!(IK.MoveLeftHand(nextNode.leftHand.transform, handDuration))) { done = false; } if (!(IK.MoveLeftFoot(nextNode.leftFoot.transform, footDuration))) { done = false; } if (done) { currentLeft = nextNode; currentNode = nextNode; lastMove = LEFT; lastIndex = nodeIndex; moving = false; } } } }
private void calculateNextNode() { if (currentNode.neighbours[nodeIndex]) { nextNode = currentNode.neighbours[nodeIndex]; } else if (currentNode.neighbours[(nodeIndex + 1) % 8]) { nodeIndex = (nodeIndex + 1) % 8; if (currentRight == currentLeft) { nextNode = currentNode.neighbours[nodeIndex]; } else { if (nextMove == RIGHT) { nextNode = currentLeft.neighbours[nodeIndex]; } else if (nextMove == LEFT) { nextNode = currentRight.neighbours[nodeIndex]; } nextNode = currentNode; } } else if (currentNode.neighbours[Mathf.Abs((nodeIndex - 1) % 8)]) { nodeIndex = (nodeIndex - 1) % 8; if (currentRight == currentLeft) { nextNode = currentNode.neighbours[nodeIndex]; } else { if (nextMove == RIGHT) { nextNode = currentLeft.neighbours[nodeIndex]; } else if (nextMove == LEFT) { nextNode = currentRight.neighbours[nodeIndex]; } nextNode = currentNode; } } else { nextNode = currentNode; } moving = true; }
//Physics Functions public override void OnTriggerEnter(Collider collider) { if (!inTransition) { if (collider.tag == "ClimbingNode") { ClimbingNode node = collider.gameObject.GetComponent <ClimbingNode>(); if (node) { stateManager.ChangeState(new NodeClimbingState(data, node)); } } } }
protected override void UpdateInput() { if (!moving) { moveX = Input.GetAxisRaw("Horizontal"); moveY = Input.GetAxisRaw("Vertical"); moveDirection = new Vector3(moveX, moveY, 0); //data.UseStamina(8 * Time.deltaTime); //!climbing || if (data.Stamina <= 0 || (!currentNodes[0] && !currentNodes[1]) || (!currentNodes[0].Active && !currentNodes[1].Active) || (!currentNodes[0].gameObject.activeInHierarchy && !currentNodes[1].gameObject.activeInHierarchy)) { stateManager.ChangeState(new PlayerWalkingState(data)); } if (Input.GetButtonDown("Jump")) { Jump(); } else if (moveDirection.magnitude > Mathf.Epsilon) { nodeIndex = Mathf.RoundToInt(Mathf.Atan2(moveDirection.x, moveDirection.y) / Mathf.PI * 4); if (nodeIndex < 0) { nodeIndex += 8; } movePolarity = FindNextMove(); ClimbingNode NextNode = FindNextNode(); if (NextNode && NextNode as ClimbingNode) { moving = true; data.StartCoroutine(Climb(NextNode as ClimbingNode)); } else { Collider col = SurfaceCheck(); if (col) { stateManager.ChangeState(new SurfaceClimbingState(data, col.transform)); } } } } }
public ClimbState(GameObject player, ClimbingNode node) : base(player) { MovementSpeed = 2f; IK.GlobalWeight = 1; IK.SetInitialIKPositions(node.rightHand, node.leftHand, node.rightFoot, node.leftFoot); currentNode = node; currentRight = node; currentLeft = node; anim.SetBool("climbing", true); UpdateAnimator(); rb.velocity = Vector3.zero; }
//Physics Functions public override void OnTriggerStay(Collider other) { if (!inTransition && climbing && data.Stamina > 0) { if (other.tag == "ClimbingNode") { ClimbingNode node = other.gameObject.GetComponent <ClimbingNode>(); if (node) { stateManager.ChangeState(new NodeClimbingState(data, node)); } } else if (other.tag == "ClimbingSurface") { stateManager.ChangeState(new SurfaceClimbingState(data, other.transform)); } } }
private void CalculateNodeNeighbors() { Collider[] NodeTriggers = Physics.OverlapSphere(transform.position, DetectionRadius); ClimbingNode[] detectedNodes = new ClimbingNode[NodeTriggers.Length]; for (int i = 0; i < NodeTriggers.Length; i++) { ClimbingNode checkNode = NodeTriggers[i].GetComponent <ClimbingNode>(); if (checkNode) { if (checkNode != detectedNodes[i]) { detectedNodes[i] = checkNode; } } } CompareDirection[0] = transform.up; CompareDirection[1] = (transform.up + transform.right).normalized; CompareDirection[2] = transform.right; CompareDirection[3] = (-transform.up + transform.right).normalized; CompareDirection[4] = -transform.up; CompareDirection[5] = (-transform.up - transform.right).normalized; CompareDirection[6] = -transform.right; CompareDirection[7] = (transform.up - transform.right).normalized; ResetNeighbours(); foreach (ClimbingNode checkNode in detectedNodes) { if (checkNode && (checkNode != this)) { DetermineNeighbour(checkNode); } } }
public NodeClimbingState(PlayerData player, ClimbingNode node) : base(player) { climbing = true; currentNodes[0] = node; currentNodes[1] = node; }
private void calculateNextMove() { if (nodeIndex == 0 || nodeIndex == 4) { if ((currentRight.transform.position - currentNode.transform.position).magnitude > (currentLeft.transform.position - currentNode.transform.position).magnitude) { nextMove = RIGHT; currentNode = currentLeft; } else { nextMove = LEFT; currentNode = currentRight; } return; } else { if (lastMove == NONE || currentRight == currentLeft) { if (nodeIndex < 4) { nextMove = RIGHT; } else { nextMove = LEFT; } } else if (lastIndex == 0 || lastIndex == 4) { if (nodeIndex < 4) { nextMove = RIGHT; } else { nextMove = LEFT; } } else if (lastMove == RIGHT) { if (nodeIndex < 4) { nextMove = LEFT; } else { nextMove = RIGHT; } } else if (lastMove == LEFT) { if (nodeIndex > 4) { nextMove = RIGHT; } else { nextMove = LEFT; } } } if (nextMove == RIGHT) { currentNode = currentRight; } else if (nextMove == LEFT) { currentNode = currentLeft; } }