TileVainia Mobs turn around

So my issues is when I shoot my mobs in Tile_Vainia they turn around. I’d like them to keep moving in the same direction or moving towards the player. This is my revision of Rick’s script.
,…
public class Enemy : MonoBehaviour
{
[SerializeField] float moveSpeed = 1f;
Rigidbody2D myRigidbody;
[SerializeField] float health = 100;
[SerializeField] int scoreValue = 150;
[SerializeField] GameObject deathVFX;
[SerializeField] float durationOfExplosion = 1f;
[SerializeField] AudioClip deathSound;
[SerializeField] [Range(0, 1)] float deathSoundVolume = 0.75f;
public GameObject drop;//whatever item you want mob to drop after dying
void Start()
{
myRigidbody = GetComponent();
}

void Update()
{
    myRigidbody.velocity = new Vector2(moveSpeed, 0f);
}

void OnTriggerExit2D(Collider2D other)
{
    moveSpeed = -moveSpeed;
    FlipEnemyFacing();
}

void FlipEnemyFacing()
{
    transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), 1f);
}
private void OnTriggerEnter2D(Collider2D other)
{
    DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
    if (!damageDealer) { return; }
    ProcessHit(damageDealer);
}

private void ProcessHit(DamageDealer damageDealer)
{
    health -= damageDealer.GetDamage();
    damageDealer.Hit();
    if (health <= 0)
    {
        Die();
    }
}

private void Die()
{
    FindObjectOfType<GameSession>().AddToScore(scoreValue);
    Destroy(gameObject);
    Instantiate(drop, transform.position, drop.transform.rotation); //your dropped sword
    /*GameObject explosion = Instantiate(deathVFX, transform.position, transform.rotation
    Destroy(explosion, durationOfExplosion);*/
   Instantiate(deathVFX, transform.position, transform.rotation);
    AudioSource.PlayClipAtPoint(deathSound, Camera.main.transform.position, deathSoundVolume);
}

},…

Hi David,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Try to figure out why the mobs are turning around when they got hit. Once you know the reason, finding a solution will be much easier.


See also:

I didn’t do a Debug.Log because the problem was rather obvious, it’s solving the problem that is the issue. & after doing a Debug.Log I found the problem was exactly what I thought it was & am still in the same position.

I assume if I have it say
if (GetComponent<DamageDealer>())
then don’t flip.

GetComponent<DamageDealer>() without anything in front of it calls the method on the same game object as your current script instance. Is this what you wanted to do?

If the problem is obvious, the problem is basically solved. If it is not solved, you have to analyse the problem further. For instance, if you know why and when the mob turns around, you could simply add a restriction so the code which turns the mob around does not get executed.

The mob turns around due to void OnTriggerExit2D(Collider2D other)
{
moveSpeed = -moveSpeed;

     FlipEnemyFacing(); 
}

& no I’m not sure if that’s what I need. But I was thinking if it’s DamageDealer component then don’t FlipEnemyFacing.

Yes, that’s what you need. Now you have to define in which case the mob is supposed to not turn around.

I will admit my homie Justin helped me out. I didn’t realize you can’t tell unity “don’t do” you have to use ! & if else statements. After experimenting I got this.

… void OnTriggerExit2D(Collider2D other)
{
if (!gameObject.GetComponent())
return;
else
moveSpeed = -moveSpeed;
FlipEnemyFacing();
Debug.Log(“Bullet Flip”);
}

void FlipEnemyFacing()
{
    //if (!gameObject.GetComponent<DamageDealer>())
    
        transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), 1f);
        Debug.Log("FlipEnemyFacing");
        return;   
}...

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

Privacy & Terms