Exemple #1
0
    private void Shoot()
    {
        Vector3 eyeToPlayer = player.transform.position - eye.transform.position;
        float   angle       = Vector3.Angle(eye.transform.forward, eyeToPlayer);

        if (angle > 15)
        {
            isShooting = false;
            weaponAnimator.IsFiring = false;
            return;
        }

        isShooting = true;
        weaponAnimator.IsFiring = true;

        if (Time.time - lastShot < 1 / FireRate)
        {
            return;
        }


        Vector3 raycastDirection = eye.transform.forward;
        Vector3 randomRotation   = (Random.rotation.eulerAngles / 360 - new Vector3(0.5f, 0.5f, 0.5f)) * spread;

        raycastDirection = Quaternion.Euler(randomRotation) * raycastDirection;

        bool collision = Physics.Raycast(eye.transform.position, raycastDirection, out RaycastHit hitInfo, 100);

        if (!collision)
        {
            return;
        }

        weaponAnimator.InstantiateShot(hitInfo);

        Health healthObject = hitInfo.transform.gameObject.GetComponent <Health>();

        if (healthObject != null)
        {
            healthObject.ReceiveDamage(ShotDamage);
        }


        lastShot = Time.time;
    }
Exemple #2
0
    // Update is called once per frame
    void Update()
    {
        bool fire = Input.GetButton("Fire1");

        CalculateSpread(fire);

        crosshair.rectTransform.localScale = new Vector3(1, 1, 1) * (spread / maxSpread) * 5 + new Vector3(1, 1, 1);

        animator.IsFiring       = fire;
        animator.shotsPerSecond = shotsPerSecond;

        if (!fire)
        {
            return;
        }

        if (Time.time - lastFire < 1 / shotsPerSecond)
        {
            return;
        }

        Vector3 raycastDirection = camera.transform.forward;
        Vector3 randomRotation   = (Random.rotation.eulerAngles / 360 - new Vector3(0.5f, 0.5f, 0.5f)) * spread;

        raycastDirection = Quaternion.Euler(randomRotation) * raycastDirection;

        bool collision = Physics.Raycast(camera.transform.position, raycastDirection, out RaycastHit hitInfo, 100);

        if (!collision)
        {
            return;
        }

        animator.InstantiateShot(hitInfo);

        Health healthObject = hitInfo.transform.gameObject.GetComponent <Health>();

        if (healthObject != null)
        {
            healthObject.ReceiveDamage(shotDamage, Player);
        }

        lastFire = Time.time;
    }