Hello all
When exit the state and return to it
the enemy is going to the player but stopping in the last known location of the player
every time the player come near him he go back
like the last distance the player and enemy had before he lost him
Here is a video of the bug
The code is a bit different to the course but not much i dont use public much in my project and i dont want to redo my code to adjust it to nathan hope you understand
I tried Reseting the path in enter in exit
i tried moving the navmesh agent to EnemyStatemachine and calling all from there
nothing seems to fix this bug
watch the video and see
My Movement Script dont mind the knockbacks i do stuff different a bit with the coding more organized
i didnt like everything on the StateMachine
[RequireComponent(typeof(CharacterController),typeof(ForceReciver))]
public class Movement : MonoBehaviour
{
ForceReciver forceReciver;
CharacterController controller;
Vector3 moveDirection;
bool isInKnockBack = false;
float timeSinceLastKnockback = 0;
[Header("KnockBack vars")]
[SerializeField] float knockBackTime;
[SerializeField] float forcePower;
private void Awake()
{
controller = GetComponent<CharacterController>();
forceReciver = GetComponent<ForceReciver>();
}
public Vector3 GetVelocity()
{
return controller.velocity;
}
public void MoveTo(Vector3 newDirection, float deltaTime)
{
moveDirection = newDirection;
Vector3 forceDirection = forceReciver.GetMotion();
controller.Move((forceDirection + moveDirection) * deltaTime);
}
EnemyChaseState
public class EnemyChaseState : EnemyBaseState
{
Fighter fighter;
AnimatorHandler animatorHandler;
Movement mover;
NavMeshAgent agent;
private readonly int LocomotionHash = Animator.StringToHash("Locomotion");
private readonly int forwardHash = Animator.StringToHash("forward");
private const float crossFadeDuration = 0.1f;
public EnemyChaseState(EnemyStateMachine stateMachine) : base(stateMachine)
{
}
public override void Enter()
{
animatorHandler = stateMachine.GetComponent<AnimatorHandler>();
mover = stateMachine.GetComponent<Movement>();
agent = stateMachine.GetComponent<NavMeshAgent>();
agent.updatePosition = false;
agent.updateRotation = false;
animatorHandler.animator.CrossFadeInFixedTime(LocomotionHash, crossFadeDuration);
}
public override void Executue(float deltaTime)
{
if (!IsInChaseRange())
{
stateMachine.SwitchState(new EnemyIdleState(stateMachine));
}
MoveToPlayer(deltaTime);
stateMachine.FacePlayer(deltaTime);
animatorHandler.ChangeAnimationMovement(forwardHash, 1f, deltaTime);
}
public override void Exit()
{
agent.ResetPath();
agent.velocity = Vector3.zero;
}
private void MoveToPlayer(float deltaTime)
{
if (agent.isOnNavMesh)
{
agent.destination = stateMachine.GetPlayer().transform.position;
mover.MoveTo(agent.desiredVelocity.normalized * stateMachine.GetMovementSpeed(), deltaTime);
}
agent.velocity = mover.GetVelocity();
}
EnemyStateMachine.cs
public class EnemyStateMachine : StateMachine
{
[SerializeField] float chasingRange = 10f;
[SerializeField] float movementSpeed = 6f;
[SerializeField] float rotationSpeed = 10f;
GameObject player;
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
}
private void Start()
{
SwitchState(new EnemyIdleState(this));
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, chasingRange);
}
public float GetChasingRange()
{
return chasingRange;
}
public GameObject GetPlayer()
{
return player;
}
public float GetMovementSpeed()
{
return movementSpeed;
}
public void FacePlayer(float deltaTime)
{
Vector3 lookPosition = player.transform.position - transform.position;
lookPosition.y = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookPosition),
deltaTime * rotationSpeed);
}
}
EnemyIdleState.cs
public class EnemyIdleState : EnemyBaseState
{
AnimatorHandler animatorHandler;
Movement mover;
private readonly int LocomotionHash = Animator.StringToHash("Locomotion");
private const float crossFadeDuration = 0.1f;
public EnemyIdleState(EnemyStateMachine stateMachine) : base(stateMachine) { }
public override void Enter()
{
animatorHandler = stateMachine.GetComponent<AnimatorHandler>();
mover = stateMachine.GetComponent<Movement>();
animatorHandler.animator.CrossFadeInFixedTime(LocomotionHash, crossFadeDuration);
}
public override void Executue(float deltaTime)
{
mover.MoveTo(Vector2.zero, deltaTime);
if (IsInChaseRange())
{
stateMachine.SwitchState(new EnemyChaseState(stateMachine));
return;
}
stateMachine.FacePlayer(deltaTime);
animatorHandler.StopAnimationMovement(deltaTime);
}
public override void Exit() { }
}