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 MokoPlays · Jun 11, 2015 at 10:43 AM · javascriptcollisioncolliderplayerenemy

Decreasing Player Health On Collision with Enemy

I'm wondering how I can get my script to work (In Java).

My HealthBar Script is as Follows (Using Unity 4.6 Canvas for the HealthBar):

 var healthbar : GameObject;
 var health : float;
 var playerController : GameObject;
 
 function Start () {
 
 }
 
 function Update () {
     playerController = GameObject.FindGameObjectWithTag("Player");
     healthbar.transform.localScale.x = health;
 }

and my Zombie AI function to decrease the Player's Health is:

 function OnCollisionEnter(collision: Collision) {
  
     if (collision.gameObject.tag == "Player"){
         healthbar.health -= 0.1;        
     }
 }

How would I go about on the Zombie's collision with the Player decreasing the health for the Player?

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 Ekta-Mehta-D · Jun 11, 2015 at 12:39 PM 0
Share

what problem you are facing ?

avatar image Ekta-Mehta-D · Jun 11, 2015 at 12:40 PM 0
Share

First initialize health variable with some float value.

avatar image MokoPlays · Jun 12, 2015 at 05:28 AM 0
Share

The value of the Float is the X Value, which currently is 0.7. The problem I'm facing is that when the Zombie Collides with my FPSController there is no health deduction.

avatar image saravanan-P · Jun 12, 2015 at 05:49 AM 0
Share

first check whether ur if condition is working or not .. use debug.log to check..

avatar image Eno-Khaon · Jun 12, 2015 at 05:57 AM 2
Share

Also, just a note:

 if(collision.gameObject.tag == "TagName")

takes about twice as long to calculate as

 if(collision.gameObject.CompareTag("TagName"))

does.

With a small and simple script, it won't matter much, but it's still generally handy to know.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aidengaming123 · Jun 12, 2015 at 06:33 AM

Is the OnCollisionEnter on the zombie? Because it needs to be in the same script as the player's in order to change the health variable. I recommend tagging your zombie as "Enemy", and putting this on your player (not tested)

  var healthbar : GameObject;
  var health : float = 10;
  var playerController : GameObject;
  
  function Start () {
  
  }
  
  function Update () {
      playerController = GameObject.FindGameObjectWithTag("Player");
      healthbar.transform.localScale.x = health;
  }
 
 function OnCollisionEnter(collision: Collision) {
   
      if (collision.gameObject.tag == "Enemy"){
          healthbar.health -= 0.1;  
          Debug.Log(health);

   
      }
  }
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 Hamza-Abdullah · Jun 12, 2015 at 06:33 AM

Add a capsule collider to the Zombie and set it to "Is trigger" And instead of using:

 function OnCollisionEnter(collision: Collision) {
   
      if (collision.gameObject.tag == "Player"){
          healthbar.health -= 0.1;        
      }
  }

use:

 function OnTriggerEnter(collision: Collider) {
   
      if (collision.gameObject.tag == "Player"){
          healthbar.health -= 0.1f;        
      }
  }

Now should be good to go!

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 MokoPlays · Jun 13, 2015 at 05:38 AM 0
Share

I've made suggested changes and am getting error: Assets/zombieAI2.js(10,20): BCE0020: An instance of type 'healthbar' is required to access non static member 'health'.

$$anonymous$$y Scripts Currently are as Follows: Healthbar.js #pragma strict var healthbar : GameObject; var health : float = 0.7; var playerController : GameObject;

function Start () {

}

function Update () { playerController = GameObject.FindGameObjectWithTag("Player");

}

ZombieAI2.js (Just Collision Function) function OnTriggerEnter(collision: Collider) {

   if (collision.gameObject.CompareTag("Player")){
       healthbar.health -= 0.1f;        
   }

}

Anyone Have any Clues?

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

8 People are following this question.

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

Related Questions

Lose health on collision 1 Answer

Fist Punch Collision 0 Answers

Enemy detection in the light(Javascript) 1 Answer

Have script detect which collider 2 Answers

What's wrong with my code? (Collision, JS) 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