2018-04-22 23:50:39 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class Bullet : MonoBehaviour {
|
|
|
|
|
[SerializeField]
|
|
|
|
|
protected float speed = 1;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
int damage = 0;
|
2018-04-23 21:25:35 +02:00
|
|
|
|
[SerializeField]
|
|
|
|
|
bool start = false;
|
|
|
|
|
string ownertag;
|
2018-04-23 18:17:46 +02:00
|
|
|
|
Rigidbody2D body;
|
2018-04-22 23:50:39 +02:00
|
|
|
|
// Use this for initialization
|
|
|
|
|
void Start () {
|
2018-04-23 18:17:46 +02:00
|
|
|
|
body = GetComponent<Rigidbody2D>();
|
2018-04-23 21:25:35 +02:00
|
|
|
|
}
|
2018-04-23 18:17:46 +02:00
|
|
|
|
|
2018-04-23 21:25:35 +02:00
|
|
|
|
public void StartBullet() {
|
|
|
|
|
start = true;
|
2018-04-23 18:17:46 +02:00
|
|
|
|
}
|
2018-04-22 23:50:39 +02:00
|
|
|
|
|
2018-04-23 18:38:43 +02:00
|
|
|
|
public void SetSpeed(float spd) {
|
|
|
|
|
speed = spd;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-22 23:50:39 +02:00
|
|
|
|
public void SetDamage(int dmg) {
|
|
|
|
|
damage = dmg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetOwner(GameObject ow) {
|
2018-04-23 21:25:35 +02:00
|
|
|
|
ownertag = ow.tag;
|
2018-04-22 23:50:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 18:17:46 +02:00
|
|
|
|
private void FixedUpdate() {
|
2018-04-23 21:25:35 +02:00
|
|
|
|
if ( start == false )
|
2018-04-22 23:50:39 +02:00
|
|
|
|
return;
|
2018-04-23 18:17:46 +02:00
|
|
|
|
body.MovePosition(transform.position + transform.localRotation * Vector3.up * speed * Time.fixedDeltaTime);
|
|
|
|
|
}
|
2018-04-22 23:50:39 +02:00
|
|
|
|
|
2018-04-23 18:17:46 +02:00
|
|
|
|
void OnTriggerEnter2D(Collider2D collider) {
|
2018-04-23 21:25:35 +02:00
|
|
|
|
if ( ownertag == null )
|
2018-04-22 23:50:39 +02:00
|
|
|
|
return;
|
2018-04-22 23:59:13 +02:00
|
|
|
|
|
2018-04-23 21:25:35 +02:00
|
|
|
|
if ( collider.gameObject.tag != ownertag ) {
|
2018-04-23 18:17:46 +02:00
|
|
|
|
Mob m = collider.gameObject.GetComponent(typeof(Mob)) as Mob;
|
2018-04-22 23:50:39 +02:00
|
|
|
|
if (m != null) {
|
|
|
|
|
m.InflictDamage(damage);
|
|
|
|
|
}
|
2018-04-23 21:25:35 +02:00
|
|
|
|
Destroy(gameObject);
|
2018-04-22 23:50:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|