EnemyPathFinding will flicker back and forth from flipping

I did the changes for this it is kind of working however, when I am on the opposite side of the grape enemy it flickers back and forth from facing me and the other direction really fast. and suggestions?

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class EnemyPathFinding : MonoBehaviour

{

[SerializeField] private float moveSpeed = 2f;

private Rigidbody2D rb;

private SpriteRenderer spriteRenderer;



private Vector2 enemyMoveDir;

private KnockBack knockBack;

// Start is called before the first frame update

void Awake()

{

    rb = GetComponent<Rigidbody2D>();

    knockBack = GetComponent<KnockBack>();

    spriteRenderer = GetComponent<SpriteRenderer>();

}

public void MoveEnemy(Vector2 targetPosition)

{

    enemyMoveDir = targetPosition;

}

public void StopMoving()

{

    enemyMoveDir = Vector3.zero;

}

private void FixedUpdate()

{

    if(knockBack.GettingKnockedBack) {return;}

    rb.MovePosition(rb.position + enemyMoveDir * (moveSpeed * Time.fixedDeltaTime));

    if(enemyMoveDir.x < 0)

    {

        Debug.Log("Flipped = True");

        spriteRenderer.flipX = true;

    }

    else if (enemyMoveDir.x > 0)

    {

        Debug.Log("Flipped = False");

        spriteRenderer.flipX = false;

    }

}

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Grape : MonoBehaviour, IEnemy

{

[SerializeField] private GameObject grapeProjectilePrefab;

private Animator grapeAnimator;

private SpriteRenderer spriteRenderer;

readonly int ATTACK_HASH = Animator.StringToHash("GrapeAttack");

private void Awake()

{

    grapeAnimator = GetComponent<Animator>();

    spriteRenderer = GetComponent<SpriteRenderer>();

}

public void Attack()

{

    grapeAnimator.SetTrigger(ATTACK_HASH);

    //Flip Sprite

    if(transform.position.x - PlayerController.Instance.transform.position.x < 0)

    {

        spriteRenderer.flipX = false;

    }

    else

    {

        spriteRenderer.flipX = true;

    }

}

public void SpawnProjectileAnimEvent()

{

    Instantiate(grapeProjectilePrefab, transform.position, Quaternion.identity);

}

}

I don’t know what happened but it works now. :roll_eyes:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms