Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

Recommended Posts

Hello!

This is my first post ever! I just wanted to share a simple gun script that uses C#. Probably the best script for beginners! There are two scripts, one for the gun, and one for the enemies to shoot at!

Hope u like it!

 

1st script:

Please login or register to see this code.

 

2nd Script (Target Script)
 

Please login or register to see this code.

 

There u go! those are all of the scripts! (Credit to brackeys for them.)

  • Like 2

Share this post


Link to post
Share on other sites

Oh i forgot:facepalm: ! When you add the script there is a section for a muzzle flash! Look up how to make one, and then drag your muzzle flash into the slot! the muzzle flash will play and you will deal damage to whatever you put your target script in! f

Share this post


Link to post
Share on other sites

Cool explaination. Thanks.

About muzzle flash, you can simply add - if (muzzleFlash != null) muzzleFlash.Play();

This way newbies can use your script without any addding...

Share this post


Link to post
Share on other sites
Posted (edited)
Please login or register to see this quote.

Thank You For This Contribution!

Edited by OneManArmy
Removed code from quote

Share this post


Link to post
Share on other sites

Why did you quote the post and add nothing?

Share this post


Link to post
Share on other sites

Can you also add the entire code from the series about how he reloads and everything?

Share this post


Link to post
Share on other sites
Posted (edited)
Please login or register to see this quote.

 

 

 

Here is a better script which is a little more advanced but comes with reloading, fire rate, and more! A lot of this is from Brackeys but quite a few things are from me too such as the shot sound animation etc. There also needs to be an animation so you will have to make that. If you do not want it simply remove all references to animations. If you would like to learn a little bit try to look at the script and understand it. I don't expect you to know about Coroutines or Enumerators. Just try to get a basic feel for C# through this script. Just ask for a script and I'll get it.  Hope this helped!!

 

using UnityEngine;
using System.Collections;

 

public class Guns : MonoBehaviour {
    
    public float damage = 25f;
    public float range = 100f;
    public GameObject impactEffect;
    public float hitForce = 30f;
    public float fireRate = 15f;
    public AudioSource gunshotSound;
    
    public int maxAmmo = 30;
    private int currentAmmo;
    public float reloadTime = 5f;
    private bool IsReloading = false;
    
    public Animation anim;
    
    public Camera fpsCam;
    public ParticleSystem muzzleFlash;
    
    private float nextTimeToFire = 0f;
    

    void Start () {
     anim = GetComponent<Animation>();    
     currentAmmo = maxAmmo;
    }
    
    
    // Update is called once per frame
    void Update () {    
        
        if (IsReloading)
            return;
        
        if (currentAmmo <=0f)
        {    
            StartCoroutine(Reload());
            return;
        }
        if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time + 1f/fireRate;
            SHOTSHOTSSHOTS.Play();
            Shoot();
            muzzleFlash.Play();
            anim.Play("Shoot");    
        }
    

    }
    
    IEnumerator Reload ()
    {
        IsReloading = true;
        
        Debug.Log ("Reloading...");
        
        yield return new WaitForSeconds(reloadTime);
        
        currentAmmo = maxAmmo;
        IsReloading = false;
    }
    
    void Shoot ()
    {
        
        currentAmmo--;
    
         RaycastHit hit;
         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) 
         { 
              Debug.Log(hit.transform.name);
              
              Target target = hit.transform.GetComponent<Target>();
              if (target != null)
              {
                  target.TakeDamage(damage);
              }
             
             if (hit.rigidbody != null)
             {
                hit.rigidbody.AddForce (-hit.normal * hitForce);
             }     
             
             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(impactGO, 2f);
         }        
    }    
}

Edited by TRCGamez
My pie was missing

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×