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 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(); } } } }