take collision info from an instantiated bullet

Hello and sorry for my bad English. For almost a week I am my friend were developing a third person shooter game and we just encountered a problem with the shooting system. We want the bullet which is been spawned to collide and then check if the collided object has the enemy script in it or not within the gun script itself. and this is the Gun script. Also thanks in advance.

public class GunScript : MonoBehaviour
{
public float Damage;
public float Range;
public float FireRate;
private float NextTimeToFire = 0;
public ParticleSystem MuzzleFlash;
public Camera TpsCam;
public Animator PlayerAnimator;

public Transform BulletSpawner;
public Transform CrosshairLoc;
public Rigidbody Projectile;
void Start()
{
   
}

void Update()
{
    if(Input.GetMouseButtonDown(1)) PlayerAnimator.SetBool("Aim", true);
    if (Input.GetMouseButtonUp(1)) PlayerAnimator.SetBool("Aim", false);
    if (Input.GetButton("Fire1") && Time.time >= NextTimeToFire) Shoot();
}

public void Shoot()
{
    NextTimeToFire = Time.time + 1 / FireRate;
    MuzzleFlash.Play();
    Ray ray = TpsCam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    RaycastHit hit;
    // Check whether your are pointing to something so as to adjust the direction
    Vector3 targetPoint;
    if (Physics.Raycast(ray, out hit))
        targetPoint = hit.point;
    else
        targetPoint = ray.GetPoint(1000); // You may need to change this value according to your needs
                                          // Create the bullet and give it a velocity according to the target point computed before
    var bullet = Instantiate(Projectile, BulletSpawner.transform.position, BulletSpawner.transform.rotation);
    bullet.GetComponent<Rigidbody>().velocity = (targetPoint - BulletSpawner.transform.position).normalized * 100;
}

}

Hi!

I would suggest you for that to create script and attach it to your bullet prefab which you are instantiating. The script itself will simply check for collision and if detects any collision then it will check for additional information - in your specific case you want to check for enemy script, so it can look like this:

void OnCollisionEnter(Collision collision)
{
    EnemyScript eScript = collision.collider.GetComponent<EnemyScript>();

    if(eScript != null)
    {
        // Do something
    }
}

However, you can create specific tag for your enemy and check for the tag:

void OnCollisionEnter(Collision collision)
{
    if (collision.collider.CompareTag("EnemyTag"))
    {
        // Do something
    }
}

The decision is up to you.

After enemy detected you can call specific functions, instantiate effects, destroy bullet and other stuff.


I am not sure for what reason you would like it to be checked inside your Gun script itself since it will create kinda mess and you also would need to store all bullets in List to access all of them for checking of collisions which will still be essentially kinda the same thing, but overcomplicated.


Hope it helps.