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

[SOLVED] (2D) Weird raycast issue

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

  1. PlazmaInteractive

    PlazmaInteractive

    Joined:
    Aug 8, 2016
    Posts:
    114
    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:
    Code (csharp):
    1. void Shoot()
    2. {
    3.   // Store anything that the ray hits in the enemyLayer here
    4.   RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.up, 100f, enemyLayer);
    5.  
    6.   // Shooting code here
    7.   if (Input.GetKey(KeyCode.Space) && Time.time > fireRate + lastFire)
    8.   {
    9.     //Debug.DrawLine(transform.position, transform.up * 100f, Color.cyan);
    10.     // Checks if the ray hits anything in enemyLayer
    11.     if (hit.collider != null)
    12.     {
    13.       GameObject hitInstance = Instantiate(hitParticle, hit.point, Quaternion.identity) as GameObject;
    14.       // Rotates the particle to direction of surface it hits
    15.       hitInstance.transform.up = hit.normal;
    16.       Destroy(hitInstance, 0.5f);
    17.       Debug.Log("Enemy is hit!");
    18.     }
    19.  
    20.     GameObject bulletClone = (GameObject)Instantiate(bullet, transform.position, transform.rotation);
    21.     Destroy(bulletClone, 1f);
    22.     lastFire = Time.time;
    23.     Debug.Log("Bullet shot!");
    24.   }
    25. }
    But I tried using this on my BulletController script which control my bullet's speed and direction and it didn't work:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BulletController : MonoBehaviour
    5. {
    6.   public int bulletSpeed;
    7.   public GameObject hitParticle;
    8.   public LayerMask enemyLayer;
    9.  
    10.   private Rigidbody2D bullet;
    11.  
    12.   void Start()
    13.   {
    14.     bullet = GetComponent<Rigidbody2D>();
    15.   }
    16.  
    17.   void FixedUpdate()
    18.   {
    19.     // Store anything that the ray hits in the enemyLayer here
    20.     RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.up, 100f, enemyLayer);
    21.  
    22.     //Debug.DrawLine(transform.position, transform.up * 100f, Color.cyan);
    23.     // Checks if the ray hits anything in enemyLayer
    24.     if (hit.collider != null)
    25.     {
    26.       GameObject hitInstance = (GameObject)Instantiate(hitParticle, hit.point, Quaternion.identity);
    27.       // Rotates the particle to direction of surface it hits
    28.       hitInstance.transform.up = hit.normal;
    29.       Destroy(hitInstance, 0.5f);
    30.       Debug.Log("Enemy is hit!");
    31.     }
    32.  
    33.     bullet.velocity = transform.up * bulletSpeed * Time.deltaTime;
    34.   }
    35.  
    36.   void OnTriggerEnter2D(Collider2D other)
    37.   {
    38.     if(other.gameObject.tag == "Enemy")
    39.     {
    40.       Destroy(gameObject);
    41.     }
    42.   }
    43.    
    44.   void OnBecameInvisible()
    45.   {
    46.     Destroy(gameObject);
    47.   }
    48. }
    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.
     
  2. xAmrxxxx

    xAmrxxxx

    Joined:
    Jan 5, 2016
    Posts:
    56
    i guess u need to attach a script to the bullet like this:

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D other)
    2. {
    3. if(other.gameObject.tag == "enemy")
    4. {
    5. Instantiate(particles,hit.point,Quaternion.identity);
    6. Destroy(gameObject);
    7. }
    8. }
    assuming that hit is your RayCastHit2D
     
  3. PlazmaInteractive

    PlazmaInteractive

    Joined:
    Aug 8, 2016
    Posts:
    114
    Thanks for the answer! It was such a simple fix :p
     
    Last edited: Aug 23, 2016