1. 程式人生 > >Unity(二)子彈的移動與銷燬

Unity(二)子彈的移動與銷燬

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletMove : MonoBehaviour {

	//子彈移動速度
	private float bulletTime = 5f;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		transform.Translate (Vector3.up * bulletTime * Time.deltaTime);

		//當子彈超出屏幕後銷燬
		if (transform.position.y > 5f) {
			Destroy (gameObject);
		}
	}

	//觸碰到敵機以後銷燬子彈
	void OnTriggerEnter2D(Collider2D other){
		if (other.tag == "Enemy") {
			Destroy (gameObject);
		}
	}
}