public static LockOnTarget ClosestVisibleTarget(Vector3 yourPosition) { if (visibleTargets.Count <= 0 || visibleTargets == null) { return(null); } float lowestDist = Mathf.Infinity; LockOnTarget selectedTarget = visibleTargets[0]; foreach (LockOnTarget i in visibleTargets) { float dist = Vector3.Distance(yourPosition, i.transform.position); if (dist < lowestDist) { lowestDist = dist; selectedTarget = i; } } return(selectedTarget); }
// Update is called once per frame void Update() { inputVector = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); rawInputVector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); isAttacking = anim.GetCurrentAnimatorStateInfo(0).IsName("SwordCombo"); if (isAttacking) { moveSpeedModifier = Mathf.MoveTowards(moveSpeedModifier, walkSpeedWhileAttackingMultiplier, walkSpeedModificationSpeed * Time.deltaTime); } else { moveSpeedModifier = Mathf.MoveTowards(moveSpeedModifier, 1, walkSpeedModificationSpeed * Time.deltaTime); if (playerInput.GetInput("Block")) { anim.Play("Block"); } } if (playerInput.GetInput("Attack")) { if (!isAttacking) { anim.Play("SwordCombo"); } } if (playerInput.GetInputUp("Attack")) { anim.Play("Idle"); } if (playerInput.GetInputDown("LockOn")) { if (lockTarget == null) { //if player attempts lock on, attempt to get closest visible target; LockOnTarget target = LockOnTarget.ClosestVisibleTarget(transform.position); if (target) { lockTarget = target.transform; } } else { lockTarget = null; } } if (lockTarget) { lockCamera.Priority = 10; //freeLookCamera.gameObject.SetActive(false); freeLookCamera.Priority = 0; freeLookCamera.m_BindingMode = Cinemachine.CinemachineTransposer.BindingMode.LockToTarget; freeLookCamera.m_XAxis.m_MaxSpeed = 0; } else { //freeLookCamera.gameObject.SetActive(true); lockCamera.Priority = 0; freeLookCamera.Priority = 10; freeLookCamera.m_BindingMode = Cinemachine.CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp; freeLookCamera.m_XAxis.m_MaxSpeed = 200; } // If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down if (toggleSprint) { if (playerInput.GetInputDown("Sprint")) { sprintEnabled = !sprintEnabled; } } else { sprintEnabled = playerInput.GetInput("Sprint"); } selectedMoveSpeed = (sprintEnabled ? sprintSpeed : walkSpeed) * moveSpeedModifier; }