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 Millionaiire · Mar 19, 2014 at 12:02 PM · javascriptcollidehealth

How to make health go down when objects collide

Here's my script

/Health Script/

var clockBG : Texture2D; var clockFG : Texture2D; var clockFGMaxWidth : float; //the starting width of the foreground bar var currentHealth:float;

function Start() { startTime = 10.0; clockFGMaxWidth = clockFG.width; currentHealth = 95.8; }

function Update () { if (Input.GetButtonDown("Fire1")) currentHealth -= 1; }

function OnGUI() { //this is the width that the foreground bar should be var newBarWidth:float = (currentHealth/100) * clockFGMaxWidth;

 Debug.Log("current health " + currentHealth);
 
 //a spacing variable to help us position the clock
 var gap:int = 20;
 
 GUI.BeginGroup(new Rect (Screen.width - clockBG.width - gap, 
    gap, clockBG.width, clockBG.height));
    GUI.DrawTexture(Rect (0,0, clockBG.width, clockBG.height), clockBG);
 
    GUI.BeginGroup(new Rect(5,6, newBarWidth, clockFG.height));
      GUI.DrawTexture(Rect(0,0, clockFG.width, clockFG.height), clockFG);
    GUI.EndGroup();
 GUI.EndGroup();
 }

I was wondering how do I make the health drop when an object collides with another, sort of like when two enemies hit, they attack each other. Let me know if I need to post any more information to help you answer the question.

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 ShadoX · Mar 19, 2014 at 01:09 PM 0
Share

You could use http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html to check for collisions and then adjust the health variable

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Millionaiire · Apr 07, 2014 at 12:01 PM

I tried that and found nothing, does anyone have any other suggestions?

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

21 People are following this question.

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

Related Questions

LoadLevel Health Question. 1 Answer

Health Script 1 Answer

Player Health drops fast, how to pause after damage 0 Answers

Setting Scroll View Width GUILayout 1 Answer

Die on collision doesnt work 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