PlayerShoot.Shoot C# (CSharp) Method

Shoot() private method

private Shoot ( ) : void
return void
    void Shoot()
    {
        if (!isLocalPlayer || weaponManager.isReloading)
        {
            return;
        }

        if (currentWeapon.bullets <= 0)
        {
            weaponManager.Reload();
            return;
        }

        currentWeapon.bullets--;

        Debug.Log("Remaining bullets: " + currentWeapon.bullets);

        //We are shooting, call the OnShoot method on the server
        CmdOnShoot();

        RaycastHit _hit;
        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, currentWeapon.range, mask) )
        {
            if (_hit.collider.tag == PLAYER_TAG)
            {
                CmdPlayerShot(_hit.collider.name, currentWeapon.damage, transform.name);
            }

            // We hit something, call the OnHit method on the server
            CmdOnHit(_hit.point, _hit.normal);
        }

        if (currentWeapon.bullets <= 0)
        {
            weaponManager.Reload();
        }
    }

Usage Example

    void Update()
    {
        if (!pause.IsPaused)
        {
            // Movement
            playerMovement.Move(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

            // Rotation
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                playerMovement.LookAt(hit.point);

                Debug.DrawRay(ray.origin, ray.direction * 100);
            }

            // Shooting
            if (Input.GetMouseButton(0))
            {
                playerShoot.Shoot();
            }

            // Reloading
            if (Input.GetKeyDown(KeyCode.R))
            {
                playerShoot.isReloading = true;
            }
        }
    }
All Usage Examples Of PlayerShoot::Shoot