Exemple #1
0
    public void SpawnProjectile()
    {
        GameObject projectile = projectilePool.GetNext();

        //Scaling constants for unit compatibility
        float projScale     = 0.5f;    //Projectile size scale
        float velocityScale = 1.5f;    //Muzzle velocity scale

        Vector3        shootFrom = GetBarrel().position;
        ProjectileInfo info      = projectile.GetComponent <ProjectileInfo>();

        info.startPosition = shootFrom;
        info.weaponInfo    = weaponInfo;

        AmmoTypeInfo ammoTypeInfo = info.GetComponent <AmmoTypeInfo>();

        //Projectile dimensions
        float projDiameter = ammoTypeInfo.caliber * projScale;
        float projLength   = projDiameter * (ammoTypeInfo.caliberToLength * 2.0f);

        projectile.transform.position   = shootFrom;
        projectile.transform.localScale = new Vector3(projDiameter, projLength, projDiameter);

        Rigidbody body = projectile.GetComponent <Rigidbody>();

        Vector3 direction = new Vector3(0, 0, weaponInfo.muzzleVelocity * velocityScale);

        projectile.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(90.0f, 0.0f, 0.0f);
        body.velocity = gameObject.transform.rotation * direction;

        AudioSource audioSource = gameObject.GetComponent <AudioSource>();
        AudioClip   fireClip    = ammoTypeInfo.soundOnFire;

        audioSource.PlayOneShot(fireClip);
    }