void Update() { Handhold nextHandhold = null; Vector3 handPos = leftHand.transform.position; if (!movingLeftHand) { handPos = rightHand.transform.position; } if (Input.GetKeyDown("w")) { nextHandhold = HandholdManager.Instance.NearestHandhold(Handhold.ButtonType.W, handPos, transform.position.y); } else if (Input.GetKeyDown("a")) { nextHandhold = HandholdManager.Instance.NearestHandhold(Handhold.ButtonType.A, handPos, transform.position.y); } else if (Input.GetKeyDown("s")) { nextHandhold = HandholdManager.Instance.NearestHandhold(Handhold.ButtonType.S, handPos, transform.position.y); } else if (Input.GetKeyDown("d")) { nextHandhold = HandholdManager.Instance.NearestHandhold(Handhold.ButtonType.D, handPos, transform.position.y); } if (nextHandhold != null && (nextHandhold.transform.position - transform.position).sqrMagnitude <= Mathf.Pow(maxArmDistance, 2)) { if (movingLeftHand) { leftHand.transform.position = nextHandhold.transform.position; } else { rightHand.transform.position = nextHandhold.transform.position; } movingLeftHand = !movingLeftHand; Vector3 newPosition = (leftHand.transform.position + rightHand.transform.position) / 2; newPosition.y -= bodyBelowHands; leftHand.transform.position -= (newPosition - transform.position); rightHand.transform.position -= (newPosition - transform.position); transform.position = newPosition; } }
public Handhold NearestHandhold(Handhold.ButtonType buttonType, Vector3 fromPosition, float aboveY) { GameObject[] handholds = GameObject.FindGameObjectsWithTag("Handhold"); Handhold nearHandhold = null; float minSqrDist = 0; for (int i = 0; i < handholds.Length; i++) { Handhold handhold = handholds[i].GetComponent <Handhold>(); float sqrDist = (handholds[i].transform.position - fromPosition).sqrMagnitude; if ((handhold != null && handhold.buttonType == buttonType && handhold.transform.position.y > aboveY) && (nearHandhold == null || sqrDist < minSqrDist)) { nearHandhold = handhold; minSqrDist = sqrDist; } } return(nearHandhold); }