45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class GoombaAI : hasHP
|
|
{
|
|
Rigidbody2D rb;
|
|
|
|
public GameObject itemDrop;
|
|
|
|
private Vector2 dir;
|
|
|
|
private Vector2 length;
|
|
void Start()
|
|
{
|
|
length = GetComponent<SpriteRenderer>().bounds.size;
|
|
curHP = MaxHP;
|
|
rb = gameObject.GetComponent<Rigidbody2D>();
|
|
dir = new Vector2(-1, 0);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
transform.rotation = Quaternion.Euler(0, rb.velocity.x < 0 ? 180 : 0, 0);
|
|
if (rb.velocity.magnitude < 0.1) { rb.velocity += new Vector2(0, 5f); }
|
|
bool hit = Physics2D.Raycast(gameObject.transform.position, dir, (length.x / 2) + 0.1f, 1 << LayerMask.NameToLayer("Ground")) || !Physics2D.Raycast(gameObject.transform.position + (Vector3.right * dir.x * ((length.x / 2) + 0.1f)), Vector3.down, (length.y / 2) + 0.1f, 1 << LayerMask.NameToLayer("Ground"));
|
|
if(hit) {
|
|
dir *= -1;
|
|
}
|
|
rb.velocity = dir + new Vector2(0,rb.velocity.y);
|
|
}
|
|
|
|
private void OnCollisionEnter2D(Collision2D collision) {
|
|
if (collision.collider.name.Equals("Player")) {
|
|
collision.collider.gameObject.GetComponent<PlayerStats>().Die();
|
|
}
|
|
}
|
|
|
|
public override void die() {
|
|
gameObject.GetComponent<Dropper>().Ammo();
|
|
Object.Destroy(gameObject);
|
|
}
|
|
|
|
}
|