THIS CONTENT DOWNLOAD SHORTLY

Objective

Main objective of this post is to give you an idea about How to work with Fire bullet with trail effect.

 

 

Step 1 Introduction

Fire bullet with trail effect is often require in gaming creating gun that shoots bullet followed by a trail. Trail effect can be implemented by using trail renderer component in unity.

You will get Final Output:

 

Step 2 Trail Renderer

Trail Renderer is used behind a moving object in the scene.

Properties that used for creating this trail renderer:

Properties Description
Material Default material can be used or new material can also be created, for this example shader of material is set to Particle/Addictive.
Time Length of trail given in seconds.
Start Width Width at the time of starting trail.
End Width Width at the time of end trail.

Other properties of trail renderer are kept default.

 

Step 3 Implementation

 

3.1 Create Bullet

  1. Take an bullet object and add Box Collider, Rigidbody and BulletScript (Given below).
  2. Code is added to destroy bullet if it collides with wall.
 

3.2 BulletScript

using UnityEngine;
using System.Collections;
 
public class BulletScript : MonoBehaviour
{
//bullet will destroy when collided with wall
void OnCollisionExit(Collision collisionInfo)
     {
        if (collisionInfo.transform.CompareTag("Wall")
   Destroy(gameObject);
    }
}
 

3.3 Creation Trail

  1. Create an empty game object and add trail renderer component to it.
  2. Set property of trail renderer as given below.

    set property
  3. It is important to attach created trail in child of bullet to follow the path of bullet.

    pathofbullet
 

3.4 Create Gun

  1. Place a gun object that will rotated according to touch.
  2. To specify bullet instantiation position place an empty object bullet position as child of gun.

    creategun
 

3.5 Code to shoot bullet having trail

This script will illustrate a gun firing a bullet according to touch position. Create an empty gameobject and attach this script to it.

Used objects in Script:

gun Move according to touch, this object can  be replaced as per game requirement.
bulletPosition Position of bullet from where bullet will be fired.
ShootIcon Icon will appear on touch down and to denote target of bullet
using UnityEngine;
using System.Collections;
 
public class MainScript : MonoBehaviour
{
 
    public GameObject gun, bulletPosition, bulletPrefeb, shotIcon;
 
 
    private int speedOfBullet = 2500;
 
    private bool moving = false;
    private Vector3 initialPosition, movePosition;
    private float angleInDegrees = 0;
 
    
 
  
void Start()
    {
        loadInitialData();
    }
 
    public void loadInitialData()
    {
//initially gunman will have angel 0
        gun.transform.eulerAngles = new Vector3(0, 0, 0);
        shotIcon.renderer.enabled = false;
 
    }
 
    void Update()
    {
        checkForTouchActions();
    }
 
  
private void checkForTouchActions()
    {
        if (Input.GetMouseButtonDown(0))
        {
 
            moving = true;
 
//if touched shoot icon will be appear
 
            shotIcon.renderer.enabled = true;
 
//calculation for angles
 
            initialPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
            double angle2 = Mathf.Atan2(initialPosition.y - gun.transform.position.y,
            initialPosition.x - gun.transform.position.x);
 
            angleInDegrees = (float)(angle2);
            angleInDegrees = Mathf.Rad2Deg * angleInDegrees;
 
            gun.transform.eulerAngles = new Vector3(0, 0, angleInDegrees);
 
        }
// if touch is moving on screen
        if (moving)
        {
            
                movePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                shotIcon.transform.position = new Vector3(movePosition.x, movePosition.y, -5);
                double angle1 = Mathf.Atan2(gun.transform.position.y - initialPosition.y,
                gun.transform.position.x - initialPosition.x);
                double angle2 = Mathf.Atan2(gun.transform.position.y - movePosition.y,
                gun.transform.position.x - movePosition.x);
 
                angleInDegrees = (float)(angle2 - angle1);
                angleInDegrees = Mathf.Rad2Deg * angleInDegrees;
 
// setting angles according to move
 
                gun.transform.eulerAngles += new Vector3(0, 0, angleInDegrees);
 
                initialPosition = movePosition;
 
            
        }
//on touch up bullet will be fired
        if (Input.GetMouseButtonUp(0))
        {
 
            shotIcon.renderer.enabled = false;
 
            if (moving == true)
fireBullet(bulletPosition.transform.position);
            
            moving = false;
        }
    }
 
//method for firing bullet
 
    private void fireBullet(Vector3 pos)
    {
 
        GameObject currentBullet = (GameObject)Instantiate(bullet, pos, Quaternion.identity);
        Vector3 angle = gun.transform.eulerAngles;
        currentBullet.transform.eulerAngles = angle;
 
        Vector3 forceVector = currentBullet.transform.right;
 
        currentBullet.rigidbody.AddForce(forceVector * speedOfBullet * 1.5f, ForceMode.Impulse);
    }
}

I hope you found this blog helpful while working Fire Bullet with Trail Effect in Unity. Let me know if you have any questions or concerns regarding Unity, please put a comment here and we will get back to you ASAP.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.

I am game programmer, always had keen interest in games which lead me to learn and develop games. I’ve been developing games for over a year. I develop games using andangine for android and unity3d for all platform compatibility.

face mask Belial The Demon Headgear Pocket Staff Magic