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);
}
},…