Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Oct 24, 2016 at 04:03 AM by dbellamy for the following reason:

I solved the issue

avatar image
0
Question by dbellamy · Oct 21, 2016 at 07:28 AM · gameobjectraycastshooter

Survival shooter enemy not taking damage

After finishing step 7 of survival shooter my enemy will not take damage from player. The enemy can damage the player please help. I have my player tagged and player and my enemy is on the shootable layer. below is my enemy health and playershoot script scripit. /enemy health script/

 using UnityEngine;
 
 namespace CompleteProject
 {
     public class EnemyHealth : MonoBehaviour
     {
         public int startingHealth = 100;            // The amount of health the enemy starts the game with.
         public int currentHealth;                   // The current health the enemy has.
         public float sinkSpeed = 2.5f;              // The speed at which the enemy sinks through the floor when dead.
         public int scoreValue = 10;                 // The amount added to the player's score when the enemy dies.
         public AudioClip deathClip;                 // The sound to play when the enemy dies.
 
 
         Animator anim;                              // Reference to the animator.
         AudioSource enemyAudio;                     // Reference to the audio source.
         ParticleSystem hitParticles;                // Reference to the particle system that plays when the enemy is damaged.
         CapsuleCollider capsuleCollider;            // Reference to the capsule collider.
         bool isDead;                                // Whether the enemy is dead.
         bool isSinking;                             // Whether the enemy has started sinking through the floor.
 
 
         void Awake ()
         {
             // Setting up the references.
             anim = GetComponent <Animator> ();
             enemyAudio = GetComponent <AudioSource> ();
             hitParticles = GetComponentInChildren <ParticleSystem> ();
             capsuleCollider = GetComponent <CapsuleCollider> ();
 
             // Setting the current health when the enemy first spawns.
             currentHealth = startingHealth;
         }
 
 
         void Update ()
         {
             // If the enemy should be sinking...
             if(isSinking)
             {
                 // ... move the enemy down by the sinkSpeed per second.
                 transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
             }
         }
 
 
         public void TakeDamage (int amount, Vector3 hitPoint)
         {
             // If the enemy is dead...
             if(isDead)
                 // ... no need to take damage so exit the function.
                 return;
 
             // Play the hurt sound effect.
             enemyAudio.Play ();
 
             // Reduce the current health by the amount of damage sustained.
             currentHealth -= amount;
             
             // Set the position of the particle system to where the hit was sustained.
             hitParticles.transform.position = hitPoint;
 
             // And play the particles.
             hitParticles.Play();
 
             // If the current health is less than or equal to zero...
             if(currentHealth <= 0)
             {
                 // ... the enemy is dead.
                 Death ();
             }
         }
 
 
         void Death ()
         {
             // The enemy is dead.
             isDead = true;
 
             // Turn the collider into a trigger so shots can pass through it.
             capsuleCollider.isTrigger = true;
 
             // Tell the animator that the enemy is dead.
             anim.SetTrigger ("Dead");
 
             // Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing).
             enemyAudio.clip = deathClip;
             enemyAudio.Play ();
         }
 
 
         public void StartSinking ()
         {
             // Find and disable the Nav Mesh Agent.
             GetComponent <NavMeshAgent> ().enabled = false;
 
             // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
             GetComponent <Rigidbody> ().isKinematic = true;
 
             // The enemy should no sink.
             isSinking = true;
 
             // Increase the score by the enemy's score value.
             ScoreManager.score += scoreValue;
 
             // After 2 seconds destory the enemy.
             Destroy (gameObject, 2f);
         }
     }
 }

/PLayer shoot script/

 using UnityEngine;
 
 public class PlayerShooting : MonoBehaviour
 {
     public int damagePerShot = 20;                  // The damage inflicted by each bullet.
     public float timeBetweenBullets = 0.15f;        // The time between each shot.
     public float range = 100f;                      // The distance the gun can fire.
 
     float timer;                                    // A timer to determine when to fire.
     Ray shootRay;                                   // A ray from the gun end forwards.
     RaycastHit shootHit;                            // A raycast hit to get information about what was hit.
     int shootableMask;                              // A layer mask so the raycast only hits things on the shootable layer.
     ParticleSystem gunParticles;                    // Reference to the particle system.
     LineRenderer gunLine;                           // Reference to the line renderer.
     AudioSource gunAudio;                           // Reference to the audio source.
     Light gunLight;                                 // Reference to the light component.
     float effectsDisplayTime = 0.2f;                // The proportion of the timeBetweenBullets that the effects will display for.
 
     void Awake()
     {
         // Create a layer mask for the Shootable layer.
         shootableMask = LayerMask.GetMask("Shootable");
 
         // Set up the references.
         gunParticles = GetComponent<ParticleSystem>();
         gunLine = GetComponent<LineRenderer>();
         gunAudio = GetComponent<AudioSource>();
         gunLight = GetComponent<Light>();
     }
 
     void Update()
     {
         // Add the time since Update was last called to the timer.
         timer += Time.deltaTime;
 
         // If the Fire1 button is being press and it's time to fire...
         if (Input.GetButton("Fire1") && timer >= timeBetweenBullets)
         {
             // ... shoot the gun.
             Shoot();
         }
 
         // If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for...
         if (timer >= timeBetweenBullets * effectsDisplayTime)
         {
             // ... disable the effects.
             DisableEffects();
         }
     }
 
     public void DisableEffects()
     {
         // Disable the line renderer and the light.
         gunLine.enabled = false;
         gunLight.enabled = false;
     }
 
     void Shoot()
     {
         // Reset the timer.
         timer = 0f;
 
         // Play the gun shot audioclip.
         gunAudio.Play();
 
         // Enable the light.
         gunLight.enabled = true;
 
         // Stop the particles from playing if they were, then start the particles.
         gunParticles.Stop();
         gunParticles.Play();
 
         // Enable the line renderer and set it's first position to be the end of the gun.
         gunLine.enabled = true;
         gunLine.SetPosition(0, transform.position);
 
         // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
         shootRay.origin = transform.position;
         shootRay.direction = transform.forward;
 
         // Perform the raycast against gameobjects on the shootable layer and if it hits something...
         if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
         {
             // Try and find an EnemyHealth script on the gameobject hit.
             EnemyHealth enemyHealth = shootHit.collider.gameObject.GetComponent<EnemyHealth>();
 
             // If the EnemyHealth component exist...
             if (enemyHealth != null)
             {
                 // ... the enemy should take damage.
                 enemyHealth.TakeDamage(damagePerShot, shootHit.point);
             }
 
             // Set the second position of the line renderer to the point the raycast hit.
             gunLine.SetPosition(1, shootHit.point);
         }
         // If the raycast didn't hit anything on the shootable layer...
         else
         {
             // ... set the second position of the line renderer to the fullest extent of the gun's range.
             gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
         }
     }
 }

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image doublemax · Oct 21, 2016 at 08:34 AM 0
Share

Did you add the EnemyHealth script to the enemies?

At which point does it fail? Do you hear the shot when you fire? Check if TakeDamage() gets called (by putting a Debug.Log in there).

avatar image dbellamy · Oct 21, 2016 at 07:26 PM 0
Share

I hear the shot when I fire and the enemy health script is added to the enemy. How do I add a debug.log? Do I add it to the parameters of TakeDamage like this public void TakeDamage (int amount, Vector3 hitPoint, Debug.Log) And what am I looking for when I apply this? $$anonymous$$y enemy can damage the player, my player can shoot I hear the shots and can see the line renderer hit the enemy its at that point nothing happens. The enemy just keeps co$$anonymous$$g.@doublemax

avatar image conman79 dbellamy · Oct 21, 2016 at 08:38 PM 0
Share

Put Debug.Log inside your damage function like this:

 public void TakeDamage (int amount, Vector3 hitPoint)
          {
              Debug.Log("Hit Detected");
              // If the enemy is dead...
              if(isDead)

Then play the game and look in the lower left hand corner of Unity (where error messages etc. are shown). Or open "Console" through the window menu.

If the function is beinng called, you'll see a "Hit Detected" message. If not, then you know that function is not being called and you can move on to the next step of debugging.

Debug.Log is the most important command you'll ever learn in Unity!

avatar image dbellamy · Oct 21, 2016 at 07:46 PM 0
Share

alt text

player-settings.png (100.0 kB)
zombunny-settings.png (104.3 kB)
avatar image dbellamy · Oct 21, 2016 at 09:44 PM 0
Share

Thank you that helps tremendously, It is not detecting my take damage function.

avatar image doublemax dbellamy · Oct 22, 2016 at 12:02 AM 0
Share

That would mean that either the RayCast doesn't hit anything (line 82), or the EnemyHealth script is not found on the hit object (line 85).

Add some more Debug.Log() before these lines to see what it is.

BTW: Do you see the laser/shot and does it point in the right direction? Does it stop at the enemy or does it pass through?

avatar image dbellamy doublemax · Oct 22, 2016 at 12:06 AM 0
Share

I see the laser shot and it stops at the enemy i'm going to add some more debugging and see if I can narrow it down.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I get an object reference from a Raycast? 1 Answer

RayCasting from a Empty GameObject 1 Answer

how to get name of game object with position by raycast 1 Answer

Raycasting + setActive() 1 Answer

Raycast affecting rigid body player 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges