Пример #1
0
    /// <summary>
    /// / uses raycast point
    /// </summary>
    private void Shoot()
    {
        // increment next fire by 1
        nextFire = Time.time + fireRate;

        // call shoteffect
        StartCoroutine(ShotEffect());

        // get new rayOrigin position (middle of camera)
        Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));

        // call new raycast object
        RaycastHit hit;

        // set laser line to gunEnd position
        laserline.SetPosition(0, gunEnd.position);

        // uses fps Cam position, transform forward, the raycast of hit , and the range
        // at this point it will detect when shooting if target is hit
        if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, range))
        {
            laserline.SetPosition(1, hit.point);

            // print to console what the raycast hit
            Debug.Log(hit.transform.name);

            // call target script to create damage
            TakeDamage health = hit.collider.GetComponent <TakeDamage>();

            // check if health is null
            if (health != null)
            {
                health.DamageTaken(damage);
            }
            // can add force
            // hit.rigidbody.addforce (-hit.normal * hitforce );
        }
        else
        {
            // set laserline position as far as the weapon length
            laserline.SetPosition(1, rayOrigin + (fpsCam.transform.forward * range));
        }
    }