Hello, I am making tower defense game and I want to rotate the enemy with the direction where it’s going because the enemy move forward but the body is backward, and this is my script:
using UnityEngine;
using UnityEngine.UI;
public class Enemy : MonoBehaviour
{
public float startSpeed = 10f;
public int startHealth = 100;
private float health;
private float speed;
public int worth = 15;
private Transform target;
private int wavePointIndex = 0;
[Header("Unity Stuff")]
public Image healthBar;
public bool isDead = false;
void Start()
{
speed = startSpeed;
health = startHealth;
target = Waypoints.points[0];
}
public void TakeDamage(int amount)
{
health -= amount;
healthBar.fillAmount = health / startHealth;
if (health <= 0f && !isDead)
{
Die();
}
}
void Die()
{
isDead = true;
PlayerStats.Money += worth;
WaveSpawner.enemiesAlive--;
Destroy(gameObject);
}
void Update()
{
Vector3 dir = target.position - transform.position;
transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
if (Vector3.Distance(transform.position, target.position) <= 0.4f)
{
GetNextWayPoint();
}
}
void GetNextWayPoint()
{
if (wavePointIndex >= Waypoints.points.Length - 1)
{
EndPath();
return;
}
wavePointIndex++;
target = Waypoints.points[wavePointIndex];
}
void EndPath()
{
PlayerStats.Lives--;
WaveSpawner.enemiesAlive--;
Destroy(gameObject);
}
}
and if you know how can I rotate the body with the direction please answer me.