private void OnCollisionEnter2D(Collision2D collision) { GameObject go = collision.gameObject; ShotLife shot = go.GetComponent <ShotLife>(); if (shot) { Destroy(GetComponent <Rigidbody>()); } }
private void FireBullet() { if (shotPrefab != null) { Vector3 shipPos = this.transform.position; Vector3 mousePos = Input.mousePosition; //Vector2 direction = (Camera.main.ScreenToWorldPoint(mousePos) - shipPos); Vector2 direction = shotSpawn.transform.position - shipPosition.position; float angle = (Mathf.Atan2(direction.y, direction.x) - Mathf.PI / 2) * Mathf.Rad2Deg; Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward); GameObject obj = Instantiate(shotPrefab, shotSpawn.transform.position, rotation); obj.transform.parent = bulletContainer.transform; Rigidbody2D rb = obj.GetComponent <Rigidbody2D>(); rb.gravityScale = 0f; rb.velocity = direction.normalized * 5; ShotLife sl = obj.GetComponent <ShotLife>(); sl.mainPlayer = this; } }