I have this code for the enemy and I made it so when you hover over the enemy, the health bar will show, and when you click the enemy, it will take health off the enemy. But the OnMouseOver, OnMouseExit and OnMouseDown function don't seem to work now and they did work before. Any help much appreciated as I don't know what the issue is.
public class EnemyController : MonoBehaviour
{
private float moveInput;
public float speed;
public float stoppingDistance;
public float retreatDistance;
private Transform target;
public static float enemyHealth;
public static float enemyHealthStart;
public Transform collisionRange;
public float collisionRadius;
public float groundedRadius = 0.3f;
public LayerMask whatIsPlayer;
public LayerMask whatIsGround;
public static bool playerInRange;
private float timer;
public float timeBetweenAttacks;
public ParticleSystem BloodParticle;
public static bool justAttacked = false;
private bool facingRight = true;
public Transform groundDetectionD;
private Rigidbody2D rb;
public float jumpForce;
private bool isGrounded;
public static bool canShake;
void Start()
{
rb = GetComponent();
enemyHealth = Random.Range(10, 30);
enemyHealthStart = enemyHealth;
target = GameObject.FindGameObjectWithTag("Player").GetComponent();
GetComponent().color = Color.red;
canShake = false;
}
void Update()
{
playerInRange = Physics2D.OverlapCircle(collisionRange.position, collisionRadius, whatIsPlayer);
isGrounded = Physics2D.OverlapCircle(groundDetectionD.position, groundedRadius, whatIsGround);
if (PlayerScript.isAlive == true)
{
if (Vector2.Distance(transform.position, target.position) > stoppingDistance)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
else if (Vector2.Distance(transform.position, target.position) < stoppingDistance && Vector2.Distance(transform.position, target.position) > retreatDistance)
{
transform.position = transform.position;
}
else if (Vector2.Distance(transform.position, target.position) < retreatDistance)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, -speed * Time.deltaTime);
}
if (playerInRange == true)
{
timer += Time.deltaTime;
if (timer >= timeBetweenAttacks && playerInRange && enemyHealth > 0)
{
Attack();
}
}
if (enemyHealth <= 0)
{
Destroy(gameObject);
playerInRange = false;
}
if (!facingRight && GameObject.Find("Player").transform.position.x > transform.position.x)
Flip();
else if (facingRight && GameObject.Find("Player").transform.position.x < transform.position.x)
Flip(); }
}
void Attack()
{
timer = 0f;
justAttacked = true;
if (PlayerScript.playerHealth > 0)
{
PlayerScript.playerHealth -= 1;
PlayerScript.bloodEmission = true;
}
}
void OnMouseDown()
{
if (playerInRange == true && PlayerScript.isAlive == true)
{
enemyHealth -= 1;
BloodParticle.Play();
canShake = true;
}
}
void OnMouseOver()
{
if (PlayerScript.isAlive == true)
EnemyHealthShow.spriteshow = true;
}
void OnMouseExit()
{
if (PlayerScript.isAlive == true)
EnemyHealthShow.spriteshow = false;
}
private void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
,
↧