protected virtual void FollowPlayer() { if ((thisCharacter == null) || (_controller == null)) { return; } if ((thisCharacter.ConditionState.CurrentState == CharacterStates.CharacterConditions.Dead) || (thisCharacter.ConditionState.CurrentState == CharacterStates.CharacterConditions.Frozen)) { return; } Character _targetChara = _target.GetComponent <Character>(); if (_targetChara.ConditionState.CurrentState == CharacterStates.CharacterConditions.Dead) { AgentFollowPlayer = false; UnassignTarget(); } float distance = Mathf.Abs(_target.position.x - transform.position.x); if (!flee) { _direction = _target.position.x > transform.position.x ? 1f : -1f; } else { _direction = _target.position.x > transform.position.x ? -1f : 1f; } if (_characterRun != null && _characterRun.AbilityInitialized) { if (distance > RunDistance) { _speed = 1; _characterRun.RunStart(); } else { _characterRun.RunStop(); } } if (distance < RunDistance && distance > WalkDistance) { _speed = 1; // walk } if (distance < WalkDistance && distance > StopDistance) { _speed = distance / WalkDistance; // walk slowly } if (distance < StopDistance) { _speed = 0f; // stop } _characterHorizontalMovement.SetHorizontalMove(_speed * _direction); if ((_characterJump != null) && (AgentFollowPlayer)) { if (_controller.State.IsCollidingRight || _controller.State.IsCollidingLeft) { _characterJump.JumpStart(); } } if (_jetpack != null && _jetpack.AbilityInitialized) { if (_target.position.y > transform.position.y + JetpackDistance) { _jetpack.JetpackStart(); } else { if (thisCharacter.MovementState.CurrentState == CharacterStates.MovementStates.Jetpacking) { _jetpack.JetpackStop(); } } } }
/// <summary> /// Every frame we override parameters if needed and cast a ray to see if we're actually pushing anything /// </summary> public override void ProcessAbility() { base.ProcessAbility(); if (!CanPush || !AbilityPermitted) { return; } CheckForPushEnd(); // if we're button based we only proceed if the push button is being pressed if (ButtonBased && (_character.CharacterType == Character.CharacterTypes.Player) && (_inputManager.PushButton.State.CurrentState != MMInput.ButtonStates.ButtonPressed)) { return; } // we set our flag to false _collidingWithPushable = false; // we cast a ray in front of us to see if we're colliding with a pushable object _raycastDirection = _character.IsFacingRight ? transform.right : -transform.right; // Added offset for Raycast vertical position Vector3 raycastVerticalOffset = Vector3.zero; raycastVerticalOffset.y = (_controller.Height() / 2 * RaycastVerticalPostion); _raycastOrigin = _controller.ColliderCenterPosition + _raycastDirection * (_controller.Width() / 2) + raycastVerticalOffset; // we cast our ray to see if we're hitting something RaycastHit2D hit = MMDebug.RayCast(_raycastOrigin, _raycastDirection, DetectionRaycastLength, _controller.PlatformMask, Color.green, _controller.Parameters.DrawRaycastsGizmos); if (hit) { if (hit.collider.gameObject.MMGetComponentNoAlloc <Pushable>() != null) { _collidingWithPushable = true; } } // if we're colliding with a pushable and are in the right conditions, we start pushing if (_controller.State.IsGrounded && _collidingWithPushable && Mathf.Abs(_controller.ExternalForce.x) >= MinimumPushSpeed && _movement.CurrentState != CharacterStates.MovementStates.Pushing && _movement.CurrentState != CharacterStates.MovementStates.Jumping) { if (_movement.CurrentState == CharacterStates.MovementStates.Running) { if (_characterRun != null) { _characterRun.RunStop(); } } PlayAbilityStartFeedbacks(); _movement.ChangeState(CharacterStates.MovementStates.Pushing); } if (hit && (_movement.CurrentState == CharacterStates.MovementStates.Pushing) && (_pushedObject == null)) { _pushedObject = hit.collider.gameObject.MMGetComponentNoAlloc <Pushable>(); _pushedObject.Attach(_controller); _character.CanFlip = false; _movementMultiplierStorage = _characterHorizontalMovement.PushSpeedMultiplier; _characterHorizontalMovement.PushSpeedMultiplier = _pushedObject.PushSpeed; } if (((_controller.Speed.x > MinimumPushSpeed) && (_movement.CurrentState == CharacterStates.MovementStates.Pushing) && (_pushedObject.transform.position.x < this.transform.position.x)) || ((_controller.Speed.x < -MinimumPushSpeed) && (_movement.CurrentState == CharacterStates.MovementStates.Pushing) && (_pushedObject.transform.position.x > this.transform.position.x))) { if (!CanPull) { StopPushing(); } else { _pulling = true; } } else { _pulling = false; } }
/// <summary> /// Every frame, we make the agent move towards the player /// </summary> protected virtual void Update() { // if the agent is not supposed to follow the player, we do nothing. if (!AgentFollowsPlayer) { return; } // if the Follower doesn't have the required components, we do nothing. if ((_targetCharacter == null) || (_controller == null)) { return; } if ((_targetCharacter.ConditionState.CurrentState == CharacterStates.CharacterConditions.Dead) || (_targetCharacter.ConditionState.CurrentState == CharacterStates.CharacterConditions.Frozen)) { return; } // we calculate the distance between the target and the agent float distance = Mathf.Abs(_target.position.x - transform.position.x); // we determine the direction _direction = _target.position.x > transform.position.x ? 1f : -1f; if (_characterRun != null && _characterRun.AbilityInitialized) { // depending on the distance between the agent and the player, we set the speed and behavior of the agent. if (distance > RunDistance) { // run _speed = 1; _characterRun.RunStart(); } else { _characterRun.RunStop(); } } if (distance < RunDistance && distance > WalkDistance) { // walk _speed = 1; } if (distance < WalkDistance && distance > StopDistance) { // walk slowly _speed = distance / WalkDistance; } if (distance < StopDistance) { // stop _speed = 0f; } // we make the agent move _characterHorizontalMovement.SetHorizontalMove(_speed * _direction); if (_characterJump != null) { // if there's an obstacle on the left or on the right of the agent, we make it jump. If it's moving, it'll jump over the obstacle. if (_controller.State.IsCollidingRight || _controller.State.IsCollidingLeft) { _characterJump.JumpStart(); } } // if the follower is equipped with a jetpack if (_jetpack != null && _jetpack.AbilityInitialized) { // if the player is above the agent + a magic factor, we make the agent start jetpacking if (_target.position.y > transform.position.y + JetpackDistance) { _jetpack.JetpackStart(); } else { if (_targetCharacter.MovementState.CurrentState == CharacterStates.MovementStates.Jetpacking) { _jetpack.JetpackStop(); } } } }