Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Infinite_Gamer · Oct 10, 2014 at 06:59 PM · c#raycastenemy health

[Answered](C#) Help with dealing damage to enemy from Raycast

Hello Unity Community,

I am making a top-down shooter. I want to use Raycasting for the bullets of the guns. This is my first time using Raycasting to change an amount on another script so I am having some problems.

--Edit-- : This doesn't seem to work anymore and I did not change anything.... I got my script to change the amount of current health for one enemy, but when I duplicate that enemy and test to see if the health changed on those enemies after I shoot them, their currentHealth does not change.

--Edit-- It prints out that is has hit something so I think the problems is that the current health is not changing..

Am I doing something wrong? Please help me with this.

Here is the raycast part of my gun script:

     void Fire()
     {
         currentBulletCount --;
         Vector3 forward = muzzle.transform.TransformDirection(Vector3.forward);
         Debug.DrawRay(muzzle.transform.position, forward * range, Color.green);
 
         RaycastHit raycastBullet;
         if(Physics.Raycast(muzzle.transform.position, forward, out raycastBullet, range))
         {
             if(raycastBullet.collider.tag == "Enemy")
             {
                 enemyHealth = raycastBullet.collider.GetComponent<EnemyHealth>();//Gets the EnemyHealth script from the gameobject the ray hits
                 enemyHealth.AddjustCurrentHealth(-damage);//subtracts damge from the current enemy health
             }
             print("I hit " + raycastBullet.collider.gameObject.name);
             //Destroy(raycastBullet.collider.gameObject);
         }
     }


And here is the Enemy Health Script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyHealth : MonoBehaviour 
 {
     //Public
     public float currentHealth = 100f;
     public float maxHealth = 100f;
 
     public bool isDead;
     
     void Update() 
     {
         AddjustCurrentHealth(0);
     }
     
     public void AddjustCurrentHealth(float adj)
     {
         currentHealth += adj;
         
         if(currentHealth <= 0)
         {
             currentHealth = 0;
             isDead = true;
         }
         
         if(currentHealth > maxHealth)
         {
             currentHealth = maxHealth;
         }
         
         if(maxHealth < 1)
         {
             maxHealth = 1;
         }
     }
 }
 


Any help is welcome

Thanks for helping me :D

Comment
Add comment · Show 3
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 ThePunisher · Oct 10, 2014 at 08:59 PM 0
Share

Did you make sure the other enemies are tagged with "Enemy"? Everything seems fine in code so I don't think that's the problem.

avatar image ThePunisher · Oct 10, 2014 at 09:01 PM 0
Share

Also, one thing I noticed is that your enemies have to reach a value lower than 0 to actually die ins$$anonymous$$d of dying when their health reaches 0. $$anonymous$$ight want to update your check to be currentHealth <= 0.

avatar image Infinite_Gamer · Oct 13, 2014 at 05:55 AM 0
Share

Yes I did tag the enemy as "Enemy", that was the first thing I checked. And thanks for the notice on the below 0, I will change that right now

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Infinite_Gamer · Oct 20, 2014 at 01:22 PM

Sorry for answering my own question.

I found out what I did wrong. I put two colliders on the game object because I put a collider on a empty game object then I put a sphere (and it has a collider too) inside of it. The raycast was detecting the sphere collider which was not tagged.

The above code works perfectly fine.

Comment
Add comment · Share
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
0

Answer by Thorny2000 · Oct 11, 2014 at 03:45 AM

The code above looks fine, assume you put a breakpoint inside AddjustCurrentHealth ti check it gets called? If not on "if(raycastBullet.collider.tag == "Enemy")" line and step inside there.

There is a Unity tutorial doing pretty much the same code here too, jump to 35:30: https://www.youtube.com/watch?v=Va77Gq8Cm8o

Comment
Add comment · Show 4 · Share
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 Infinite_Gamer · Oct 13, 2014 at 05:58 AM 0
Share

I have looked at the video and it did not help. I cant see any problems because when I shoot it prints out that is has hit something.. So I am guessing that problem has to be the EnemyHealth script changing the current health...

avatar image Thorny2000 · Oct 13, 2014 at 06:34 AM 0
Share

You mention printing again, do you not like breakpoints? Put breakpoints in all the functions from the hit detection to the health variable code. Step through the code. Watch the currentHealth value. Also check your public objects are setup and not null.

Hot tip: Also, you can set a breakpoint and only have it break if a variable changes: Set a breakpoint on a line (F9). Right click on the red breakpoint circle. Breakpoint properties. Tick "break when the expression changes"

avatar image Infinite_Gamer · Oct 13, 2014 at 07:14 PM 0
Share

I haven't really ever used breakpoints, but I will give this a try.

avatar image Infinite_Gamer · Oct 13, 2014 at 08:12 PM 0
Share

I couldn't get it to work

avatar image
0

Answer by denisb · Oct 11, 2014 at 03:56 AM

There is a tutorial for that kind of game with everything you need to do it like you want http://unity3d.com/learn/tutorials/projects/survival-shooter

it is using raycast to hit the target obviously. you should go check it out.

Comment
Add comment · Show 1 · Share
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 Infinite_Gamer · Oct 13, 2014 at 04:25 AM 0
Share

Ok thanks for your fast responses. I will check out these videos. Thanks for all the help! :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

[C#] Problem with RayCast check. 1 Answer

Raycasting, LineRenderer & Layermasks 0 Answers

Using RayCast to do Continuous Shooting? 1 Answer

Firing a RayCast in a Raycast, Rayception 1 Answer


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