Exemple #1
0
    private void Shoot()
    {
        if (Input.GetMouseButton(0) && currentAmmo > 0 && canFire)
        {
            currentAmmo--;

            UI.UpdateAmmo(currentAmmo);

            muzzleFlash.SetActive(true);

            if (!weaponSound.isPlaying)
            {
                weaponSound.Play();
            }

            // Ray rayOrigin = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0));
            // points to the middle of the screen

            Ray rayOrigin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));

            // if (Physics.Raycast(rayOrigin, Mathf.Infinity)) {

            RaycastHit hitInfo;
            if (Physics.Raycast(rayOrigin, out hitInfo))
            {
                Debug.Log("Hit something:" + hitInfo.collider);

                GameObject hit = Instantiate(hitMarker, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
                // get hit memory address

                Destroy(hit, 2.0f);

                Destructible crate = hitInfo.transform.GetComponent <Destructible>();
                if (crate != null)
                {
                    crate.DestoryCrate();
                }
            }
        }
        else
        {
            muzzleFlash.SetActive(false);

            weaponSound.Stop();
        }
    }