Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[2D] Easiest way to instantiate particle upon bullet impact?

Discussion in 'Scripting' started by PlazmaInteractive, Aug 12, 2016.

  1. PlazmaInteractive

    PlazmaInteractive

    Joined:
    Aug 8, 2016
    Posts:
    114
    I've been searching online to find how to make some kind of a bullet impact but I haven't found much. So, if a bullet hits a surface of an enemy or obstacles, it will create an impact particle that plays correctly. I'm not sure how to really explain it but let's say a bullet hit a vertical wall. If the player shoots straight into it, it will bounce back straight or if it hits in an angle, the bullet will bounce back in an angle that makes sense.

    From what I've gathered so far, I have to use raycasting and I'm kind of new to it. If any of you knows how to do this, I'll appreciate if you can explain briefly about what's going on in your code. Thanks! :)
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    At the moment you instantiate your particle, you need to just rotate it to the direction the polygon is facing. You can do this using its "normal".

    Code (CSharp):
    1.  
    2.     public GameObject _particle; // Particle prefab
    3.     public LayerMask _layerMask; // What layers can the raycast hit?
    4.  
    5.     void Update()
    6.     {
    7.         // Create ray from current object's position, and point it forward - this is your bullet shot
    8.         Ray ray = new Ray(transform.position, transform.forward);
    9.  
    10.         // If ray hits something, store information in here
    11.         RaycastHit hitInfo;
    12.  
    13.         // Cast the ray, and if it hits something...
    14.         if (Physics.Raycast(ray, out hitInfo, 1000f, layerMask))
    15.         {
    16.             // Create instance/copy of your particle prefab at the point you hit in the world
    17.             GameObject particleInstance = Instantiate(_particle, hitInfo.point, Quaternion.identity) as GameObject;
    18.  
    19.             // Rotate particle instance to direction of the face that it hit
    20.             particleInstance.transform.up = hitInfo.normal;
    21.         }
    22.     }
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    To add on, you probably want to set the up vector to the reflected vector:
    http://docs.unity3d.com/ScriptReference/Vector3.Reflect.html
     
  4. PlazmaInteractive

    PlazmaInteractive

    Joined:
    Aug 8, 2016
    Posts:
    114
    I've tried using your code in my game but it doesn't seem to work? Where should I place the code exactly? I've tried putting it in my BulletController script which I use for making the bullet travel forward and checking bullet collision with enemy and it didn't worked. I tried placing it in my Shoot() method in PlayerController script but it still didn't work. So where exactly should I place it in my game?
     
  5. PlazmaInteractive

    PlazmaInteractive

    Joined:
    Aug 8, 2016
    Posts:
    114
    Actually, I found out how to do this, thanks to Stardog's script above and Brackey's video tutorial on YouTube.
    Here's the script for anyone else having trouble:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.   public float playerSpeed;
    7.   public float rotateSpeed;
    8.   public float fireRate;
    9.  
    10.   public Transform bulletSpawn;
    11.   public GameObject bullet;
    12.   public GameObject hitParticle;
    13.   public LayerMask enemyLayer;
    14.  
    15.   private Rigidbody2D player;
    16.   private float lastFire = 0f;
    17.  
    18.   void Start()
    19.   {
    20.     player = GetComponent<Rigidbody2D>();
    21.   }
    22.  
    23.   void FixedUpdate()
    24.   {
    25.     float horizontal = Input.GetAxis("Horizontal");
    26.     float vertical = Input.GetAxis("Vertical");
    27.  
    28.     // Movement
    29.     player.velocity = transform.up * playerSpeed * vertical * Time.deltaTime;
    30.     transform.Rotate(0, 0, horizontal * -rotateSpeed);
    31.  
    32.     Shoot(); // Shoot
    33.   }
    34.  
    35.   void Shoot()
    36.   {
    37.     // Store anything that the ray hits in the enemyLayer here
    38.     RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.up, 100f, enemyLayer);
    39.  
    40.     // Shooting code here
    41.     if (Input.GetKey(KeyCode.Space) && Time.time > fireRate + lastFire)
    42.     {
    43.       Debug.DrawLine(transform.position, transform.up * 100f, Color.cyan);
    44.       // Checks if the ray hits anything in enemyLayer
    45.       if (hit.collider != null)
    46.       {
    47.         GameObject hitInstance = Instantiate(hitParticle, hit.point, Quaternion.identity) as GameObject;
    48.         // Rotates the particle to direction of surface it hits
    49.         hitInstance.transform.up = hit.normal;
    50.         Destroy(hitInstance, 0.5f);
    51.         Debug.Log("Enemy is hit!");
    52.       }
    53.  
    54.       GameObject bulletClone = Instantiate(bullet, bulletSpawn.position, transform.rotation) as GameObject;
    55.       Destroy(bulletClone, 2f);
    56.       lastFire = Time.time;
    57.       Debug.Log("Bullet shot!");
    58.     }
    59.   }
    60. }