Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

How to make your Gun and Bullet system for your Unity Game and attach it to FPS Player Controller

Posted on February 12, 2021February 12, 2021 by Habibie

This is a basic tutorial about making gun and bullet projectile system for your Unity games.

First I showed you how to make a bullet object, making it moving at certain speed, then making it as a prefab so we can fire any amount of bullets as we wish.

Then I showed you how to make a gun object, how to instantiate the bullet at gun position and matching it’s orientation (rotation).

As I said, everything you see here is the basic concept. There are a lot of things need to be done to improve it, such as adding sound effect, adding appropriate gun and bullet meshes, adding muzzle flashes, etc.

About the FPS controller, you can download it from free by watching this previous video: https://www.youtube.com/watch?v=0W8rDJyp5QI The download link is in that video description.

Scripts used in this video:

TNBullet.cs

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

public class TNBullet : MonoBehaviour
{
	
	public float bulletSpeed = 10f;
	public float destroyAfterSeconds = 5f;
	
	Rigidbody bullet;
	
    // Start is called before the first frame update
    void Start()
    {
        bullet = GetComponent<Rigidbody>();
		GameObject playersGun = GameObject.Find("GunObject");
		transform.position = playersGun.transform.position;
		transform.rotation = playersGun.transform.rotation;
    }

    // Update is called once per frame
    void Update()
    {
		if(destroyAfterSeconds > 0f){
			bullet.velocity = transform.forward * bulletSpeed;
			destroyAfterSeconds -= Time.deltaTime;
		}else{
			Destroy(gameObject);
		}
    }
}

TNGun.cs

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

public class TNGun : MonoBehaviour
{
	
	public GameObject bulletPrefab;
	
	public void FireTheBullet(){
		Instantiate(bulletPrefab, new Vector3(0,0,0), Quaternion.identity);
	}
	
}
Post Views: 257
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme