[SOLVED] (2D) Weird raycast issue

I’m using raycast to determine where to instantiate hit particles when a bullet hit an enemy’s collider. This worked if I put my raycast script into my NormalShotSystem script which has a Shoot() method:

void Shoot()
{
  // Store anything that the ray hits in the enemyLayer here
  RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.up, 100f, enemyLayer);

  // Shooting code here
  if (Input.GetKey(KeyCode.Space) && Time.time > fireRate + lastFire)
  {
    //Debug.DrawLine(transform.position, transform.up * 100f, Color.cyan);
    // Checks if the ray hits anything in enemyLayer
    if (hit.collider != null)
    {
      GameObject hitInstance = Instantiate(hitParticle, hit.point, Quaternion.identity) as GameObject;
      // Rotates the particle to direction of surface it hits
      hitInstance.transform.up = hit.normal;
      Destroy(hitInstance, 0.5f);
      Debug.Log("Enemy is hit!");
    }

    GameObject bulletClone = (GameObject)Instantiate(bullet, transform.position, transform.rotation);
    Destroy(bulletClone, 1f);
    lastFire = Time.time;
    Debug.Log("Bullet shot!");
  }
}

But I tried using this on my BulletController script which control my bullet’s speed and direction and it didn’t work:

using UnityEngine;
using System.Collections;

public class BulletController : MonoBehaviour
{
  public int bulletSpeed;
  public GameObject hitParticle;
  public LayerMask enemyLayer;

  private Rigidbody2D bullet;

  void Start()
  {
    bullet = GetComponent<Rigidbody2D>();
  }

  void FixedUpdate()
  {
    // Store anything that the ray hits in the enemyLayer here
    RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.up, 100f, enemyLayer);

    //Debug.DrawLine(transform.position, transform.up * 100f, Color.cyan);
    // Checks if the ray hits anything in enemyLayer
    if (hit.collider != null)
    {
      GameObject hitInstance = (GameObject)Instantiate(hitParticle, hit.point, Quaternion.identity);
      // Rotates the particle to direction of surface it hits
      hitInstance.transform.up = hit.normal;
      Destroy(hitInstance, 0.5f);
      Debug.Log("Enemy is hit!");
    }

    bullet.velocity = transform.up * bulletSpeed * Time.deltaTime;
  }

  void OnTriggerEnter2D(Collider2D other)
  {
    if(other.gameObject.tag == "Enemy")
    {
      Destroy(gameObject);
    }
  }
   
  void OnBecameInvisible()
  {
    Destroy(gameObject);
  }
}

What happen is hitParticles is instantiated everytime on the back of my bullet which creates a kind of particle trail and I suspect it’s because I put it in FixedUpdate() so everytime it checks if hit’s collider is not equal null, it will instantiate a hit particle every frame. How do I make it so the hit particles instantiate only when it hit’s the enemy’s collider?
Thanks.

i guess u need to attach a script to the bullet like this:

void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "enemy")
{
Instantiate(particles,hit.point,Quaternion.identity);
Destroy(gameObject);
}
}

assuming that hit is your RayCastHit2D

Thanks for the answer! It was such a simple fix :stuck_out_tongue: