So far the code only supports basic hip fire and reload functions for my weapon but I have created Iron Sight sprites that I want to set up.
How I want it to work is that when the player holds the right mouse button a selection of sprite animations play leading to the full iron sight mode of the gun.
Also, I got most of this code from different tutorials from Youtube and the internet I am in no way a programmer.
Here are the sprites… (Code at the bottom)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Pistol : MonoBehaviour
{
public Sprite idlePistol;
public Sprite shotPistol;
public Sprite shotPistol1;
public Sprite shotPistol2;
public Sprite shotPistol3;
public float pistolDamage;
public float pistolRange;
public AudioClip shotSound;
public AudioClip reloadSound;
public AudioClip emptyGunSound;
public Text ammoText;
public int ammoAmount;
public int ammoClipSize;
public GameObject bulletHole;
int ammoLeft;
int ammoClipLeft;
bool isShot;
bool isReloading;
AudioSource source;
void Awake()
{
source = GetComponent<AudioSource>();
ammoLeft = ammoAmount;
ammoClipLeft = ammoClipSize;
}
void Update()
{
ammoText.text = ammoClipLeft + " / " + ammoLeft;
if (Input.GetButtonDown("Fire1") && isReloading == false)
isShot = true;
if (Input.GetKeyDown(KeyCode.R) && isReloading == false)
{
Reload();
}
}
void FixedUpdate()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (isShot == true && ammoClipLeft > 0 && isReloading == false)
{
isShot = false;
ammoClipLeft--;
source.PlayOneShot(shotSound);
StartCoroutine("shot");
if (Physics.Raycast(ray, out hit, pistolRange))
{
Debug.Log("I collided with" + hit.collider.gameObject.name);
hit.collider.gameObject.SendMessage("pistolHit", pistolDamage, SendMessageOptions.DontRequireReceiver);
Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
}
}
else if (isShot == true && ammoClipLeft <= 0 && isReloading == false)
{
isShot = false;
Reload();
}
}
void Reload()
{
int bulletsToReload = ammoClipSize - ammoClipLeft;
if (ammoLeft >= bulletsToReload)
{
StartCoroutine("ReloadWeapon");
ammoLeft -= bulletsToReload;
ammoClipLeft = ammoClipSize;
}
else if (ammoLeft < bulletsToReload && ammoLeft > 0)
{
StartCoroutine("ReloadWeapon");
ammoClipLeft += ammoLeft;
ammoLeft = 0;
}
else if (ammoLeft <= 0)
{
source.PlayOneShot(emptyGunSound);
}
}
IEnumerator ReloadWeapon()
{
isReloading = true;
source.PlayOneShot(reloadSound);
yield return new WaitForSeconds(2);
isReloading = false;
}
IEnumerator shot()
{
GetComponent<SpriteRenderer>().sprite = shotPistol;
yield return new WaitForSeconds(0.1f);
GetComponent<SpriteRenderer>().sprite = shotPistol1;
yield return new WaitForSeconds(0.1f);
GetComponent<SpriteRenderer>().sprite = shotPistol2;
yield return new WaitForSeconds(0.1f);
GetComponent<SpriteRenderer>().sprite = shotPistol3;
yield return new WaitForSeconds(0.1f);
GetComponent<SpriteRenderer>().sprite = idlePistol;
}
}



