Rifle Added in That Is My Sheep

So here it is, we added the Rifle weapon. yaaaaaay.

p11

Here is the code of our rifle weapon, and the rifle projectile.

public class Rifle : Weapon 

{

    public float MaxBulletDistance;

    public float Speed;

    public RifleProjectile ProjectileRifle;

    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;

        RifleProjectile projectile = (RifleProjectile) Instantiate (ProjectileRifle, ProjectileFireLocation.position, ProjectileFireLocation.rotation);

        projectile.Initialize (gameObject, new Vector2(direction.x, 0), 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 RifleProjectile : 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