This commit is contained in:
Gerard Gascón 2025-04-24 14:07:24 +02:00
commit a8c6025cd3
158 changed files with 86052 additions and 0 deletions

54
Assets/EnemyController.cs Normal file
View file

@ -0,0 +1,54 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour {
public float maxSpeed = 1f;
public float speed = 1f;
private Rigidbody2D rb2d;
HealthBar health;
PlayerController player;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
health = GameObject.FindGameObjectWithTag("Health").GetComponent<HealthBar>();
}
// Update is called once per frame
void FixedUpdate () {
rb2d.AddForce(Vector2.right * speed);
float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -maxSpeed, maxSpeed);
rb2d.velocity = new Vector2(limitedSpeed, rb2d.velocity.y);
if (rb2d.velocity.x > -0.01f && rb2d.velocity.x < 0.01f){
speed = -speed;
rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
}
if (speed < 0) {
transform.localScale = new Vector3(1f, 1f, 1f);
} else if (speed > 0){
transform.localScale = new Vector3(-1f, 1f, 1f);
}
}
void OnTriggerEnter2D(Collider2D col){
if(col.gameObject.tag == "Fall"){
Destroy(gameObject);
}
}
private void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "Player"){
if (health.isHealing == false){
health.SendMessage("TakeDamage", 1);
health.isHealing = true;
}
}
if (col.gameObject.tag == "Bullet"){
Destroy(gameObject);
}
}
}