Weekly C# Code Samples - Share, Download, Suggest, Ask and Receive!

Hello awesome unity developers,

i wanted to make this thread for a long time where i can help other by asking, sharing and writing scripts for newbies and beginners so, today i will be sharing a 2 good C# script one for weapon shooting system and another one for weapon switching hope you like them!

1st - Shooting System

Let’s begin by creating a script named “ShootingSystem” and write the following code :-

using UnityEngine;
using System.Collections.Generic;
using System.Collections;

public class ShootingSystem {
         
          public Transform _barrel;
          public GameObject _bullet;
          public float _fireRate; // you can use this for the animation rigged ones bellow

}

The animation rigged firing :-

using UnityEngine;
using System.Collections.Generic;
using System.Collections;

public class ShootingSystem {
         
          public Transform _barrel;
          public GameObject _bullet;
          public Animation anim; // the weapon will fire depending on how long the animation clip is

}
void Start ()
{
    // get the animation component only on start
    anim = GetComponent<Animation>();
}

void Update ()
{
    // return IF there's no _bullet or _barrel
    if (!_bullet || !_barrel)
        return;

    // shoot when the button "Fire" is pressed and the animation isn't playing
    if (Input.GetKey("Fire") && anim.isPlaying == false)
    {
        anim.Play("Fire");
        Instantiate(_bullet, _barrel.position, _barrel.forward);
    }
}
2 Likes

hi nice script looks fine. I need a c # script which allows the player to jump but only if the counter is on 2 or 1. If the counter is equal to 0, then he can´t jump. The counter may only have the value of 2 again when he has touched the ground. I need no animation only the script and where I have to write the text exactly. I hope you understand what I mean, i´m unfortunately still beginner with unity but that will / should be better.

it is a 2D game.

1 Like

Okay i will help you since beginners makes wonders when they learn how! Here’s the script

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MakePlayerJump : Monobehaviour
{
    // how high will the player check for the ground
    public float groundRayLength = 0.1f;
    // layer masks for the ground
    public LayerMask groundLayerMask
    // player jump speed
    player float  jumpSpeed;
    // the counter that you want
    public int jumps = 2;


    // first of all check for the ground
    public bool isGrounded {
        get {
                Vector3 position = transform.position;
                // you can specify any type of collider
                position.y = GetComponent<Collider2D>().bounds.min.y + 0.1f;
                // get the ray length
                float length = groundRayLength + 0.1f;
                // set a boolean to return true or false
                bool grounded = Physics2D.Raycast (position, Vector3.down, length, groundLayerMask.value);

                return grounded;
            }
    }
    // now the jumping part
    void Update ()
    {
        // the riggedbody component
        var  rb2d = GetComponent<Rigidbody>();
        var velocity = rb2d.velocity;

        if (isGrounded)
           {
            jumps = 2;
           }

        if (Input.GetKey(Keycode.Space) && jumps > 0)
        {
            jumps -= 1;
            velocity.y = JumpSpeed;
        }

        rb2d.velocity = velocity;
    }
}

you can add you own method to increase or decrease the jumps count (e.x timer, collision)

thanks for posting on the forum!

1 Like

Welcome Back to WC#CS!

Today i will share the reloading system script

here’s how it works :

using UnityEngine;
using System.Collections.Generic;
using System.Collections;

public class ReloadScript : MonoBehaviour
{
   
    public int _maxAmmo;
    public int _currAmmo;
    bool isReloading = false;
   
    // current player reload second
    public float reloadTime = 0f;
    // max time to reload
    public float counter = 10f;
   
    void Update()
    {
        // check if the player is currently reloading
        if (isReloding)
        {
            reloadTimer += 1f;
        }
       
        // check if the player presses the reload button
        if (_currAmmo < _maxAmmo && Input.GetKey("r") && !isReloading)
        {
            isReloading = true;
        }
       
        // auto reload
        if (_currAmmo <= 0 && !isReloding)
        {
            isReloding = true;
        }
       
        // now reload
        if (reloadTimer > counter && isReloding)
        {
            reloadTimer = 0;
            Reload();
        }
    }
   
    void Reload ()
    {
        var ammoNeeded = _maxAmmo - _currAmmo;
        _currAmmo = _currAmmo + ammoNeeded;
        isReloding = false;
    }
}

thanks for visitng my forum and good luck!

1 Like