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 Weatas · Nov 13, 2014 at 12:07 AM · enemynotdecreasedoeshealth

Enemy health not decreasing

Okay so I have a melee system and a enemy health system, they both work and let me play in game, however the enemy's health just does not decrease for some reason?

Here are both scripts

ENEMY HEALTH

pragma strict

ar Health = 100;

function ApplyDammage (TheDammage : int) { Health -= TheDammage; }

MELEE SYSTEM

  #pragma strict
 var TheDammage : int = 50;
 var Distance : float;
 var MaxDistance : float = 1.5;
 function Update()
 {
 if (Input.GetButtonDown ("Fire1"))
 {
 var hit : RaycastHit;
 if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit))
 {
 Distance = hit.distance;
 if (Distance < MaxDistance)
 {
 hit.transform.SendMessage("ApplyDamage", TheDammage, SendMessageOptions.DontRequireReceiver);
 }
 }
 }
 }
Comment
Add comment · Show 6
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 MrSoad · Nov 13, 2014 at 12:07 AM 0
Share

Try this :

 #pragma strict
 
 var TheDammage : int = 50;
 var Distance : float;
 var $$anonymous$$axDistance : float = 1.5;
 
 function Update() {
 
     if (Input.GetButtonDown ("Fire1")) {
     
         var hit : RaycastHit;
         
         if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit)) {

             Debug.Log("Raycast is working");
             Distance = hit.distance;
             
             if (Distance < $$anonymous$$axDistance) {
             
                 hit.collider.gameObject.Send$$anonymous$$essage("ApplyDamage", TheDammage, Send$$anonymous$$essageOptions.DontRequireReceiver);
             }
         }
     }
 }

You can get rid of the Debug.Log line if it works, I've put it in as a check to make sure that the raycast is hitting something. You may get an error if it does not hit what you expect it to hit, in which case you need to do a check for the correct object (an if statement) around your send message code(Which I thought may be wrong in your code above and have changed).

avatar image Weatas · Nov 13, 2014 at 12:24 AM 0
Share

The new script Works the same as the old one which I have now binned, however enemy's health for some reason still does not decrease?

avatar image MrSoad · Nov 13, 2014 at 12:25 AM 0
Share

Did the Debug Window show the message "Raycast is working" when you ran the script?

avatar image Weatas · Nov 13, 2014 at 12:40 AM 0
Share

I have changed script, and deleted debug.log line, however still for some reason there is no decrease in health? Ray-cast is working alright, I got a big list of ray-cast is working, I entered the game yet enemy's health is still not effected, the melee system works, gives me distance reading, I just can't get my head around it? I started writing up extra's on the enemy health script so the enemy disappears (death)

pragma strict

var Health = 100;

function update () { if(Health <= 0) Dead(); }

function ApplyDammage (TheDammage : int) { Health -= TheDammage; }

function Dead() { Destroy (gameObject); }

avatar image Weatas · Nov 13, 2014 at 01:02 AM 0
Share

Yes! that done it, thank you so much, health reaches zero now however doesn't $$anonymous$$us, I see why this doesn't occur because your not telling the enemy to take any past the 0 point, this I prefer, perfect.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MrSoad · Nov 13, 2014 at 12:47 AM

Ok, so the problem is either the distance check(set the MaxDistance to 5 for a last test) which seems unlikly, or it is the send message. Change the send massage line to this :

 hit.collider.gameObject.SendMessage("ApplyDamage");

And change this script to this :

 #pragma strict
 
 var Health : int = 100;
 
 function ApplyDamage() {
 
     Health = Health - 50;
     
     if (Health <= 0) {
     
         Debug.Log("I have sustained a lethal injury...");
         Health = 0;
     }
 }

Making sure this script is on the collider object of the target, if the collider is on a child and this script is on the parent then you will have to script up through the objects hierarchy to find the the object that has this script attached(in your other script).

lol, I've just spotted the probable cause and corrected it, Damage was written as Dammage in your ApplyDamage function, fixed this typo, now give it a go :)

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Script Wont Take away health from my player when hit by enemy bullet. 0 Answers

Lose health on collision 1 Answer

(C#) Enemy health and take damage from bullets 1 Answer

I want to rotate the model to face player, not bend? 1 Answer

Melee Damage script by collision 2 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