Good morning everyone,
As the title says I have a flying enemy that works like it is intended to but messes up. My enemy detects the player via OverlapCircle and he does what he is supposed to move towards the player and then once the player is in the raycast for shooting it shoots. Here is the problem, when the player changes the way they are facing the enemy goes bonkers. It stutters in place and changes the way it faces constantly. When the player gets out of the Overlap circle the enemy starts moving away from the player and not towards it. Here is the code that is related to this issue.
void Vision()
{
playerInRange = Physics2D.OverlapCircle(transform.position, playerRange, 1 << LayerMask.NameToLayer("Player"));
spotted = Physics2D.Linecast(sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("Player"));
// flyingStart = Physics2D.Linecast(flightStart.position, flightEnd.position, 1 << LayerMask.NameToLayer("Player"));
if (spotted == true)
{
SetState(AerialFighterStates.FIRE);
//Debug.Log("I should fire!");
if (playerInRange == true)
{
SetState(AerialFighterStates.PATROL);
// Debug.Log("I should Start flying!");
}
}
else
{
SetState(AerialFighterStates.IDLE);
//Debug.Log("I should be in idle if out of shooting range");
}
if (playerInRange == true)
{
SetState(AerialFighterStates.PATROL);
// Debug.Log("I should Start flying!");
if (spotted == true)
{
SetState(AerialFighterStates.FIRE);
//Debug.Log("I should fire!");
}
}
else
{
SetState(AerialFighterStates.IDLE);
//Debug.Log("I should be in idle if out of flying range!");
}
}
void Patrol()
{
transform.position = Vector3.MoveTowards(transform.position, thePlayer.transform.position, moveSpeed * Time.deltaTime);
colliding = Physics2D.Linecast(sightStart.position, wallEnd.position, detectWhat);
if (colliding == true)
{
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
moveSpeed *= -1;
facingRight = !facingRight;
}
if((thePlayer.transform.position.x < transform.position.x && thePlayer.transform.localScale.x < 0) || (thePlayer.transform.position.x > transform.position.x && thePlayer.transform.localScale.x > 0))
{
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
moveSpeed *= -1;
facingRight = !facingRight;
return;
}