- Home /
Enemy is moving opposite of the player 2D
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;
}
I think it comes from the code where you check on which side the player is, you might have meant to use 'transform.localScale' ins$$anonymous$$d of 'thePlayer.transform.localScale'. It's bit hard to follow so i may have got it wrong.
Additionally you could simplify your Vision methods 'if else' clauses. This should do the same with less confusion:
if (spotted == true)
{
SetState(AerialFighterStates.FIRE);
//Debug.Log("I should fire!");
}
else 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 flying range!");
}
Unless there is some mysterious magic in 'SetState()', then this might not work. Hope this helps, cheers.
Answer by Cuttlas-U · Sep 27, 2017 at 01:30 PM
hi;
maybe multiple the move speed by a negative number is changing the direction :
moveSpeed *= -1;
that was it! Thank you it's always the simple things that I miss lol All I had to do was change the moveSpeed to 1 ins$$anonymous$$d of -1.
Your answer
![](https://koobas.hobune.stream/wayback/20220612134238im_/https://answers.unity.com/themes/thub/images/avi.jpg)