Beispiel #1
0
    void RaycastShot()
    {
        Transform camera = cameraMovement.transform; //Shoot from camera, not gun

        GunObject gun = gunHandler.gun;

        Vector3 shotDir   = getShotDir(bulletSpread);
        Vector3 worldDir  = camera.TransformDirection(shotDir);
        Vector3 impactPos = camera.position;

        impactPos += worldDir * gun.bulletRange;

        Ray shotRay = new Ray(camera.position, worldDir);

        if (Physics.Raycast(shotRay, out var hit, gun.bulletRange, collisionLayer))
        {
            DamageZone damaged = null;
            float      dis     = Vector3.Distance(camera.position, hit.point) + 0.05f;
            if (Physics.Raycast(shotRay, out var dmg, dis, damageLayer))
            {
                damaged = dmg.transform.GetComponent <DamageZone>();
            }

            impactPos = hit.point;
            CreateImpact(impactPos, hit.normal);
            if (damaged != null) //If we hit something we should damage
            {
                if (!damaged.DamageableAlreadyDead())
                {
                    bool killed = damaged.Damage(gun.bulletDamage, gun.headshotMult);
                    if (ui)
                    {
                        ui.ShowHitmarker(killed);    //Damages and shows hitmarker
                    }
                }
            }
        }

        Vector3[]       pos   = { gunHandler.bulletSpawn.transform.position, impactPos };
        ShotTrailHelper trail = helper.getAvailableTrail();

        trail.Initialize(pos);
    }
    ShotTrailHelper createTrail()
    {
        GameObject trail = new GameObject();

        trail.layer = LayerMask.NameToLayer(trailLayer);

        trail.transform.name = "ShootTrail";
        LineRenderer line = trail.AddComponent <LineRenderer>() as LineRenderer;

        if (cloneSettings != null)
        {
            line.material       = cloneSettings.material;
            line.numCapVertices = cloneSettings.numCapVertices;
            line.widthCurve     = cloneSettings.widthCurve;
            line.startColor     = cloneSettings.startColor;
            line.endColor       = cloneSettings.endColor;
        }

        ShotTrailHelper helper = trail.AddComponent <ShotTrailHelper>() as ShotTrailHelper;

        helper.Initialize();
        return(helper);
    }