public class SpawnBullet : MonoBehaviour
{
public GameObject prefabBullett;
public Transform centerBullett; // empty gameObject
GameManager _gameManager;
void Start()
{
_gameManager = FindObjectOfType<GameManager>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GameObject bullett= Instantiate(prefabBullett, centerBullett.position, centerBullett.rotation);
Rigidbody rb = bullett.GetComponent<Rigidbody>();
rb.AddForce(centerBullett.forward*10,ForceMode.Impulse);
Destroy (bullett,2);
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Bullett")
{
Destroy(gameObject); //delete yourself
Destroy(collision.gameObject);
}
}