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 /
avatar image
0
Question by reto · Aug 01, 2011 at 01:10 PM · collisionregeneratehealth

Sidescroller health bar collision script with regen?

I am at University in Leeds making a side scroller game for kids. Here is my health script i know it works but i need to make it take off health on a collision with my barrels in my scene. The barrels have a mesh colliders and the character has a character controller attached as well as other scripts for other things. My health bar is updated in the GUI when i test it but i can't get the collision to work. Also how to regenerate the health once my re spawn script takes him back to the start. Many thanks for having a look at this!

var h00 : Texture2D; var h25 : Texture2D; var h50 : Texture2D; var h75 : Texture2D; var h100 : Texture2D;

static var HEALTH = 100;

function Update() { var g_Health = gameObject.Find("g_Health");

 if(HEALTH > 75)
 {
     g_Health.guiTexture.texture = h100;
     return;
 }
 else if (HEALTH > 50)
 {
     g_Health.guiTexture.texture = h75;
     return;
 }
 else if (HEALTH > 25)
 {
     g_Health.guiTexture.texture = h50;
     return;
 }
 else if (HEALTH > 10)
 {
     g_Health.guiTexture.texture = h25;
     return;
 }
 else if (HEALTH <= 0)
 {
     g_Health.guiTexture.texture = h00;
     return;
 }

}

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 Aldwoni_legacy · Aug 01, 2011 at 02:06 PM 0
Share

Have you tested the collision? does it work? do you have an error? what does your healthbar when it when HEALTH = 5? When you take him back to start simply set HEALTH = 100. if you want to give him live again

avatar image reto · Aug 01, 2011 at 02:54 PM 0
Share

I don't know how to add the collision to this script and to script the take health on collision. So i haven't tested it only tested with print in the console window to see if the script works but don;t know how to implement it in the script. thanks for your time

avatar image Aldwoni_legacy · Aug 01, 2011 at 02:55 PM 0
Share

You need to add a OnCollisionEnter I expect.

avatar image reto · Aug 01, 2011 at 03:08 PM 0
Share

var h00 : Texture2D; var h25 : Texture2D; var h50 : Texture2D; var h75 : Texture2D; var h100 : Texture2D;

static var HEALTH = 100;

function Update() { var g_Health = gameObject.Find("g_Health");

 if(HEALTH > 75)
 {
     g_Health.guiTexture.texture = h100;
     return;
 }
 else if (HEALTH > 50)
 {
     g_Health.guiTexture.texture = h75;
     return;
 }
 else if (HEALTH > 25)
 {
     g_Health.guiTexture.texture = h50;
     return;
 }
 else if (HEALTH > 10)
 {
     g_Health.guiTexture.texture = h25;
     return;
 }
 else if (HEALTH <= 0)
 {
     g_Health.guiTexture.texture = h00;
     return;
 }

}

function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.collider.gameObject.tag == "barrel") { take health??? } }

Collision added like this? how would i add the take damage scripting into the colission?

avatar image Aldwoni_legacy · Aug 01, 2011 at 03:13 PM 0
Share

you can change health by: "HEALTH = HEALTH-1;"

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Techsuport · Aug 19, 2011 at 02:48 AM

 //what i did was draw the bar with 
 var maxHealth:float;
 var health:float;
 var healthRegen:float;
 var pic:Texture2D;//fills the bar
 var barRect:Rect=new Rect(0,0,.1,.5);//height and width are multuplyed be screen space
 //remember not to leave things at 0
 function OnGUI(){
     GUI.Label(Rect(barRect.x,barRect.y,barRect.width*screen.width,barRect.hieght*screen.height),pic);//
 }
 function FixedUpdate(){
     heath+=healthRegen*Time.deltaTime;//makes health regenerate at healthRegen per-second ;)
 },        //what i did was draw the bar with 
     var maxHealth:float;
     var health:float;
     var healthRegen:float;
     var pic:Texture2D;//fills the bar
     var barRect:Rect=new Rect(0,0,.1,.5);//height and width are multuplyed be screen space
     //remember not to leave things at 0
     function OnGUI(){
         GUI.Label(Rect(barRect.x,barRect.y,barRect.width*screen.width,barRect.hieght*screen.height),pic);//
     }
     function FixedUpdate(){
         heath+=healthRegen*Time.deltaTime;//makes health regenerate at healthRegen per-second ;)
     }
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 Techsuport · Aug 19, 2011 at 03:22 AM 0
Share

also.. health-=25;//subtracts 25

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

Two gameObjects on collision one increase the health and the other decrease the health of the player. 2 Answers

How do i set health to regenerate over a specified range of time? 1 Answer

Ai that applies damage in collision? 1 Answer

Health regen script wont work properly 2 Answers

For loop on collision (two hit kill) 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