private void TargetStar() { // Then branch: the user has just entered Star target mode. // Just move to the nearest available star. if (!_targetStar) { _targetStar = locker.GetNearestAvailableStar(maxAllowedDistance); if (!_targetStar) { return; } viewfinder.gameObject.transform.position = _targetStar.transform.position; } else { // A star is already targeted. If the first star has not already been selected, let the user choose it. // else, let the user choose the next available star if (_selectedStars.Count == 0) { var pointedStar = locker.GetFirstStarByRaycast(viewfinder.transform, maxAllowedDistance, _tr); if (pointedStar) { _targetStar = pointedStar; viewfinder.gameObject.transform.position = pointedStar.transform.position; } } else { var pointedStar = locker.GetAvailableStarByRaycast(viewfinder.transform, maxAllowedDistance); if (pointedStar) { _targetStar = pointedStar; viewfinder.gameObject.transform.position = pointedStar.transform.position; } } } }
/*private void OnDrawGizmos() * { * Gizmos.color = Color.red; * Gizmos.DrawRay(_position, _selectedStar.GetComponent<Rigidbody2D>().position - _position); * }*/ void Update() { _position = _rb.position; HookInput(); if (_controller.IsGrounded()) { _waitTillGrounded = false; } if (_wantToHook && !_selectedStar) { _selectedStar = locker.GetTargetedStar(); _nextStar = _selectedStar; } if (_wantToHook && _selectedStar) { var target = locker.GetAvailableStarByRaycast(viewfinder.transform, _maxSelectDistance); if (target) { _nextStar = target; attackJoystick.AutoTargetWorking(false); } // Update viewfinder position viewfinder.gameObject.transform.position = _nextStar.transform.position; } if (_selectedStar) { Vector2 relativePosition = _selectedStar.GetComponent <Rigidbody2D>().position - _position; RaycastHit2D hit = Physics2D.Raycast(_position, relativePosition, relativePosition.magnitude, obstacleLayerMask); if (hit.collider) { if (_joint.enabled) { DestroyJoint(); _waitTillGrounded = true; _controller.ToggleHook(); } return; } } // If there is joint and either hook button is released or Kin is on the ground, destroy it if (_joint.enabled) { Vector2 starCenteredRelativePosition = _position - _joint.connectedBody.position; // If the character is hanged to the star and is not moving, disable friction if remainder movement is very little, in // order to make it do little oscillations around the center of the hanging ray... if (starCenteredRelativePosition.y <= -_joint.distance + 0.05f) { _friction.maxForce = 0; } // ... otherwise re-enable friction to slow character down when the player is not moving else { _friction.maxForce = 1f; } if (_joint.maxDistanceOnly && starCenteredRelativePosition.magnitude >= _minHangDistance - 0.01f) { _joint.maxDistanceOnly = false; } if (!_wantToHook || _controller.IsGrounded()) // || _skyIsMoving) { _waitTillGrounded = false; DestroyJoint(); _controller.ToggleHook(); } } else { // If there is not a joint and hook is pressed... if (_wantToHook) { var temp = locker.GetStarsInRange(_maxHangDistance); _availableStars .ForEach(x => { if (!temp.Contains(x)) { x.DeHighlightStar(); } }); _availableStars = temp; _availableStars.ForEach(x => x.HighlightStar()); if (_availableStars.Count == 0 || !_selectedStar) { return; } if (_selectedStar) { // If Kin is NOT the ground and meanwhile jump is pressed assume that the hang has to be accomplished. // Create a joint, start the effect. if (!_controller.IsGrounded() && ((Vector2)_selectedStar.transform.position - _position).magnitude < _maxHangDistance && !_waitTillGrounded) { attackJoystick.SetHanging(true); Rigidbody2D otherRb = _selectedStar.GetComponent <Rigidbody2D>(); _joint.connectedBody = otherRb; _joint.distance = (_position - otherRb.position).magnitude; _joint.maxDistanceOnly = _joint.distance < _minHangDistance; if (_joint.maxDistanceOnly) { _joint.distance = _minHangDistance; } _friction.maxForce = 1; _joint.enabled = true; _friction.enabled = true; hangingEffect.StartEffect(otherRb.transform); _controller.ToggleHook(); } } } } }