53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class BulletController : MonoBehaviour {
|
|
[SerializeField] GameObject explosion;
|
|
|
|
[SerializeField] int damage = 1;
|
|
[SerializeField] public int speed = 60; // ppf
|
|
|
|
Rigidbody2D body;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
}
|
|
|
|
void Die() {
|
|
Instantiate(explosion);
|
|
explosion.transform.position = transform.position;
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
public void InitializeBullet(bool down, float angle = 0) {
|
|
body = GetComponent<Rigidbody2D>();
|
|
if (down) {
|
|
transform.localScale = new Vector2(1.0f, -1.0f);
|
|
body.velocity = new Vector2(
|
|
-1.0f * speed * Mathf.Sin(angle),
|
|
-1.0f * speed * Mathf.Cos(angle));
|
|
} else {
|
|
body.velocity = new Vector2(
|
|
speed * Mathf.Sin(angle),
|
|
speed * Mathf.Cos(angle));
|
|
}
|
|
GetComponent<Rigidbody2D>().SetRotation(angle*-180f/Mathf.PI);
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D collision) {
|
|
if (collision.gameObject.CompareTag("PegLegIoEnemy")) {
|
|
collision.gameObject.GetComponent<PegLegIoEnemyController>().Hurt(damage);
|
|
Die();
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate()
|
|
{
|
|
if (Mathf.Abs(transform.position.x) > 480 || Mathf.Abs(transform.position.y) > 270) {
|
|
Die();
|
|
}
|
|
}
|
|
}
|