I am currently working on a platformer and I am having this problem my enemies where their AI throws them out of the level instead of moving from side to side on the platform they are on. I used this tutorial
. this is my script, any help would be very much appreciated
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemyAI : MonoBehaviour
{
public float speed;
// public float distance;
public int maxHealth = 100;
int currentHealth;
private bool movingRight = true;
public Transform groundDetection;
public ParticleSystem blood;
// public Transform player;
void Start()
{
currentHealth = maxHealth;
//player = GameObject.FindGameObjectWithTag("Player").transform;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
if(currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log("Enemy died");
blood.Play();
Destroy(gameObject);
}
void Update()
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
/* RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
if(groundInfo.collider == false)
{
if(movingRight == true)
{
transform.eulerAngles = new Vector3(0, -180, 0);
movingRight = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
movingRight = true;
}
}*/
}
}
.