Search Unity

How do I make a gun have iron sight aim using pixel sprites?

Discussion in 'General Graphics' started by Deleted User, Jan 28, 2020.

  1. Deleted User

    Deleted User

    Guest

    Hey Everyone!

    I am new to programming and Unity for that matter and I am playing around with pixel gun sprites that I made.

    Anyways, so far I only have non-aim gun animations for shooting and I want to add to my code that I have made to support aiming down the sites when you hold down right-click on your mouse. My code is below as well as the images.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(AudioSource))]
    6. public class Pistol : MonoBehaviour
    7. {
    8.  
    9.     public Sprite idlePistol;
    10.     public Sprite shotPistol;
    11.     public Sprite shotPistol1;
    12.     public Sprite shotPistol2;
    13.     public Sprite shotPistol3;
    14.     public float pistolDamage;
    15.     public float pistolRange;
    16.     public AudioClip shotSound;
    17.     public AudioClip reloadSound;
    18.     public AudioClip emptyGunSound;
    19.  
    20.     public Text ammoText;
    21.  
    22.     public int ammoAmount;
    23.     public int ammoClipSize;
    24.  
    25.     public GameObject bulletHole;
    26.  
    27.     int ammoLeft;
    28.     int ammoClipLeft;
    29.  
    30.     bool isShot;
    31.     bool isReloading;
    32.  
    33.     AudioSource source;
    34.  
    35.     void Awake()
    36.     {
    37.         source = GetComponent<AudioSource>();
    38.         ammoLeft = ammoAmount;
    39.         ammoClipLeft = ammoClipSize;
    40.     }
    41.  
    42.     void Update()
    43.     {
    44.         ammoText.text = ammoClipLeft + " / " + ammoLeft;
    45.  
    46.         if (Input.GetButtonDown("Fire1") && isReloading == false)
    47.             isShot = true;
    48.         if (Input.GetKeyDown(KeyCode.R) && isReloading == false)
    49.         {
    50.             Reload();
    51.         }
    52.     }
    53.  
    54.     void FixedUpdate()
    55.     {
    56.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    57.         RaycastHit hit;
    58.         if (isShot == true && ammoClipLeft > 0 && isReloading == false)
    59.         {
    60.             isShot = false;
    61.             ammoClipLeft--;
    62.             source.PlayOneShot(shotSound);
    63.             StartCoroutine("shot");
    64.             if (Physics.Raycast(ray, out hit, pistolRange))
    65.             {
    66.                 Debug.Log("Wszedlem w kolizje z " + hit.collider.gameObject.name);
    67.                 hit.collider.gameObject.SendMessage("pistolHit", pistolDamage, SendMessageOptions.DontRequireReceiver);
    68.                 Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
    69.             }
    70.         }
    71.         else if (isShot == true && ammoClipLeft <= 0 && isReloading == false)
    72.         {
    73.             isShot = false;
    74.             Reload();
    75.         }
    76.     }
    77.  
    78.     void Reload()
    79.     {
    80.         int bulletsToReload = ammoClipSize - ammoClipLeft;
    81.         if (ammoLeft >= bulletsToReload)
    82.         {
    83.             StartCoroutine("ReloadWeapon");
    84.             ammoLeft -= bulletsToReload;
    85.             ammoClipLeft = ammoClipSize;
    86.         }
    87.         else if (ammoLeft < bulletsToReload && ammoLeft > 0)
    88.         {
    89.             StartCoroutine("ReloadWeapon");
    90.             ammoClipLeft += ammoLeft;
    91.             ammoLeft = 0;
    92.         }
    93.         else if (ammoLeft <= 0)
    94.         {
    95.             source.PlayOneShot(emptyGunSound);
    96.         }
    97.     }
    98.     IEnumerator ReloadWeapon()
    99.     {
    100.         isReloading = true;
    101.         source.PlayOneShot(reloadSound);
    102.         yield return new WaitForSeconds(2);
    103.         isReloading = false;
    104.     }
    105.     IEnumerator shot()
    106.     {
    107.         GetComponent<SpriteRenderer>().sprite = shotPistol;
    108.         yield return new WaitForSeconds(0.1f);
    109.         GetComponent<SpriteRenderer>().sprite = shotPistol1;
    110.         yield return new WaitForSeconds(0.1f);
    111.         GetComponent<SpriteRenderer>().sprite = shotPistol2;
    112.         yield return new WaitForSeconds(0.1f);
    113.         GetComponent<SpriteRenderer>().sprite = shotPistol3;
    114.         yield return new WaitForSeconds(0.1f);
    115.         GetComponent<SpriteRenderer>().sprite = idlePistol;
    116.     }
    117.  
    118. }
    I used the internet and youtube to help make some of this code ;)

    Also, how do you make the shots perfectly line up through the iron sights?

    Here is the sprite work for the iron sights...

    a1.png a2.png a3.png a4.png a5.png

    Thanks!