• 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 Alaro · Mar 14, 2012 at 08:44 PM · 2dplatformerhealthbarhealth

Health subtracts when hit by bullit

Hey i'm currently trying to make a 2D platformer, and i've reached the stage where i want to make the enemies work out, so what i'm currently strangling with is to make a script that subtracts my 1 of my ¨hearths¨ ( I have made a GUI-texture and placed out some hearths, and i've also made a enemy that shoots towards my character)

Here's my healthcontroll script so far :

var health1 : Texture2D; //one life left

var health2 : Texture2D; //two life left

var health3 : Texture2D; //full heatlh

static var LIVES = 3;

function Update (){

 switch(LIVES)

 {    

 case 3:

     guiTexture.texture = health3;

 break;

 

 case 2:

     guiTexture.texture = health2;

 break;

 

 case 1: 

     guiTexture.texture = health1;

 break;

 

 case 0:

     //game over script here

 break;

 }

}

So i know i'll have to make script that will subtract my ¨LIVES¨, does anyone have a example of such a script? would love to get this scripting done :)

Comment
Add comment · Show 1
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 syclamoth · Mar 14, 2012 at 09:27 PM 0
Share

Why have you shown us the part of the script that is working perfectly? It's kind of irrelevant. How do your bullets work? Do they follow a predeter$$anonymous$$ed path, or are they physics based?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GutoThomas · Mar 15, 2012 at 02:04 AM

you can call a function in this script to detect any collision by doing:

  function OnCollisionEnter(collider: Collision) {
     LIVES--;
     }


or if you want to tag your Sphere to detect just when it hits the player you can attach a tag called "Sphere" in the sphere and make an if statement inside OnCollisionEnter function to check the tag of the collider, like:

   function OnCollisionEnter(collider: Collision) {
     if(collider.transform.tag == "Sphere") {
     LIVES--;
     }
     }
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 Alaro · Mar 15, 2012 at 01:37 AM

The script was just to illustrate how far i've gotten, and the bullets are physic-based ¨spheres¨.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Why does my OnCollisionEnter2D not work? 3 Answers

2D jump with Raycast 1 Answer

[2D][Beginner]How do I heal my character when pressing a key while next to a health pack? 1 Answer

Stoping character with Platformer Controller script attached. 2,5D ladder. 1 Answer

2D flash health and damage system 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