using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerWeapon : MonoBehaviour { public GameObject bulletPrefab; public Transform bulletSpawn; public float bulletSpeed = 30; public float lifeTime = 3; // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.Space)) { Fire(); } } private void Fire() { GameObject bullet = Instantiate(bulletPrefab); Physics.IgnoreCollision(bullet.GetComponent(), bulletSpawn.parent.GetComponent()); bullet.transform.position = bulletSpawn.position; var rot = bullet.transform.rotation.eulerAngles; bullet.transform.rotation = Quaternion.Euler(rot.x, transform.eulerAngles.y, rot.z); bullet.GetComponent().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse); StartCoroutine(DestroyBulletAfterTime(bullet, lifeTime)); } private IEnumerator DestroyBulletAfterTime(GameObject bullet, float delay) { yield return new WaitForSeconds(delay); Destroy(bullet); } }