0
votes

In survival Shooter, I have the enemy object and added my own 3d game object Everything is working fine, the gameobject is following and hurting the player but when the player shoots the gameobject the bullet goes through the gameobject and score also does not increase. I have attached the rigidbody component to my custom 3d gameobject but the bullet still doesn't cause any damage.

This is the code for PlayerShooting
using UnityEngine;

public class PlayerShooting : MonoBehaviour
{
    public int damagePerShot = 20;
    public float timeBetweenBullets = 0.15f;
    public float range = 100f;


    float timer;
    Ray shootRay;
    RaycastHit shootHit;
    int shootableMask;
    ParticleSystem gunParticles;
    LineRenderer gunLine;
    AudioSource gunAudio;
    Light gunLight;
    float effectsDisplayTime = 0.2f;


    void Awake ()
    {
        shootableMask = LayerMask.GetMask ("Default");
        shootableMask = LayerMask.GetMask ("Default");
        shootableMask = LayerMask.GetMask("Default");
        shootableMask = LayerMask.GetMask("Default");
        gunParticles = GetComponent<ParticleSystem> ();
        gunLine = GetComponent <LineRenderer> ();
        gunAudio = GetComponent<AudioSource> ();
        gunLight = GetComponent<Light> ();
    }


    void Update ()
    {
        timer += Time.deltaTime;

        if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets)
        {
            Shoot ();
        }

        if(timer >= timeBetweenBullets * effectsDisplayTime)
        {
            DisableEffects ();
        }
    }


    public void DisableEffects ()
    {
        gunLine.enabled = false;
        gunLight.enabled = false;
    }


    void Shoot ()
    {
        timer = 0f;

        gunAudio.Play ();

        gunLight.enabled = true;

        gunParticles.Stop ();
        gunParticles.Play ();

        gunLine.enabled = true;
        gunLine.SetPosition (0, transform.position);

        shootRay.origin = transform.position;
        shootRay.direction = transform.forward;

        if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
        {
            EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
            if(enemyHealth != null)
            {
                enemyHealth.TakeDamage (damagePerShot, shootHit.point);
            }
            gunLine.SetPosition (1, shootHit.point);
        }
        else
        {
            gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
        }
    }
}

and below is the enemy health script:

    using UnityEngine;

public class EnemyHealth : MonoBehaviour
{
    public int startingHealth = 100;
    public int currentHealth;
    public float sinkSpeed = 2.5f;
    public int scoreValue = 10;
    public AudioClip deathClip;


    Animator anim;
    AudioSource enemyAudio;
    ParticleSystem hitParticles;
    CapsuleCollider capsuleCollider;
    bool isDead;
    bool isSinking;


    void Awake ()
    {
        anim = GetComponent <Animator> ();
        enemyAudio = GetComponent <AudioSource> ();
        hitParticles = GetComponentInChildren <ParticleSystem> ();
        capsuleCollider = GetComponent <CapsuleCollider> ();

        currentHealth = startingHealth;
    }


    void Update ()
    {
        if(isSinking)
        {
            transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
        }
    }


    public void TakeDamage (int amount, Vector3 hitPoint)
    {
        if(isDead)
            return;

        enemyAudio.Play ();

        currentHealth -= amount;

        hitParticles.transform.position = hitPoint;
        hitParticles.Play();

        if(currentHealth <= 0)
        {
            Death ();
        }
    }


    void Death ()
    {
        isDead = true;

        capsuleCollider.isTrigger = true;

        anim.SetTrigger ("Dead");
        ScoreManager.score += scoreValue;
        enemyAudio.clip = deathClip;
        enemyAudio.Play ();
    }


    public void StartSinking ()
    {
        GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;
        GetComponent <Rigidbody> ().isKinematic = true;
        isSinking = true;
        ScoreManager.score += scoreValue;
        Destroy (gameObject, 2f);
    }
}
1
before diving into the code, shouldnt there be an OntriggerEnter() or OnCollisionEnter() somewhere in your code if you expect things to take place in that kind of events?rustyBucketBay
Followed tutorial from Unity's Website, I'm new to Game DevelopmentTameem

1 Answers

0
votes

If the line that shows the bullet is going through the enemy gameobject, then your Physics.Raycast is not hitting this gameobject's collider.

You can test this quickly by adding some Debug output to your raycast block as below:

    if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
    {
        Debug.Log("I HIT SOMETHING CALLED: " + shootHit.collider.gameObject.name);
        EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
        if(enemyHealth != null)
        {
            enemyHealth.TakeDamage (damagePerShot, shootHit.point);
        }
        gunLine.SetPosition (1, shootHit.point);
    }
    else
    {
        Debug.Log("I DIDN'T HIT ANYTHING!");
        gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
    }

I suspect your problem is that your enemy gameobject is on the Default layer.

In your Awake method, you are setting your shootableMask as follows shootableMask = LayerMask.GetMask ("Default"); and then when you call Physics.Raycast you are passing it the shootableMask. What this is effectively doing is telling the raycast to ignore any objects that are on the Default layer.

Try putting your enemy gameobject on a different layer.

On a side note, you only need to have shootableMask = LayerMask.GetMask ("Default"); once in your Awake method, not four times.