Shotgun Added in That is my sheep game

We added a shotgun so we can save ourselves from the beasts.

We will add a rifle too.

p10

Here is the code of our Shotgun Weapon and Shotgun projectile

public class Shotgun : Weapon 

{

    public float MaxBulletDistance;

    public float Speed;

    public ShotgunProjectile ProjectileShotgun;

    private Vector2 _velocity;

    private Player _player;

    void Start()

    {

        ProjectileFireLocation = WeaponManager.WeaponLocation.transform;

    }

    void Update()

    {

        _canFireIn -= Time.deltaTime;

    }

    public void Fire(GameObject gameObject, Vector2 direction, Vector2 Velocity)

    {

        if (_canFireIn >= 0)

            return;

        ShotgunProjectile projectile = (ShotgunProjectile) Instantiate (ProjectileShotgun, ProjectileFireLocation.position, ProjectileFireLocation.rotation);

        projectile.Initialize (gameObject, new Vector2(direction.x, 0), Velocity);

        ShotgunProjectile projectile2 = (ShotgunProjectile) Instantiate (ProjectileShotgun, ProjectileFireLocation.position, ProjectileFireLocation.rotation);

        projectile2.Initialize (gameObject, new Vector2(direction.x, 0.45f), Velocity);

        ShotgunProjectile projectile3 = (ShotgunProjectile) Instantiate (ProjectileShotgun, ProjectileFireLocation.position, ProjectileFireLocation.rotation);

        projectile3.Initialize (gameObject, new Vector2(direction.x, –0.45f), Velocity);

        ShowFireWeaponEffect();

        //AudioSource.PlayClipAtPoint (WeaponShootSound, transform.position);

        _canFireIn = FireRate;

    }

    private void ShowFireWeaponEffect()

    {

        if (FireWeaponEffect != null)

        {

            var effect = (GameObject)Instantiate (FireWeaponEffect, ProjectileFireLocation.position, ProjectileFireLocation.rotation);

            effect.transform.parent = transform;

        }

    }

}

public class ShotgunProjectile : Projectile 

{

    public float       MaxDistanceToLive;

    public int             Damage;

    public bool IsHit;

    public bool IsMiss;

    private Transform _projectileSpawner;

    public void Start()

    {

        _projectileSpawner = Weapon.ProjectileFireLocation;

    }

    public void Update ()

    {

        if ( (MaxDistanceToLive -= Time.deltaTime * Speed) <= 0)

        {

            DestroyProjectile();

        }

        transform.Translate ( Direction * ((Mathf.Abs(InitialVelocity.x) + Speed) * Time.deltaTime), Space.World );

        Vector3 diff = (_projectileSpawner.transform.position – transform.position);

        _distanceFromPlayer = Mathf.Abs(diff.x);

    }

    protected override void OnCollideOther(Collider2D other)

    {

        Collider2D myCollider = GetComponent<Collider2D>();

        myCollider.enabled = false;

    }

    protected override void OnCollideTakeDamage( Collider2D other, ITakeDamage takeDamage)

    {

        takeDamage.TakeDamage(Damage, gameObject);

        Collider2D myCollider = GetComponent<Collider2D>();

        myCollider.enabled = false;

    }

    protected override void OnCollideKill( Collider2D other, IKillable kill)

    {

        kill.KillAnimal(gameObject);

        Collider2D myCollider = GetComponent<Collider2D>();

        myCollider.enabled = false;

        DestroyProjectile();

    }

    private void DestroyProjectile()

    {

        Destroy (gameObject);

    }

}

Thanks for reading.

if you want to check out some of our projects please subscribe to our YouTube channel:

You can check up our work in progress games here:

 

If you are looking for solutions to game development issues see our blogs here:

 

If you like our projects please sign up for our newsletter at the bottom of this Page.

Leave a Reply