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 sandhillceltic · Mar 05, 2013 at 03:26 AM · collisionmeshhealth

I have a problem with implementing a health and death system, help?

Ok, so I am designing a basic game that has creatures chasing you, and you have to jump to dodge them. The creatures are suppose to damage the player when they come into contact with the player, and after ten or so hits, the player dies. I am new to Unity, and have a very limited knowledge on JavaScript. All my code is in JavaScript, so keep that in mind. I have tried all the scripts I wrote, and all the ones I could find on the internet. What I really need is a simple mesh colliding damage system, with health and damage displayed as a variable. The script that I wrote at the start was a single code, applied to the player, and having the variables of "what mesh would cause damage on collision", "health of player", and "damage caused by a single collision of said mesh" (not in those exact words). Can you tell me how you would do it, if I was on the right track, and any tips and tricks involving this sort of collision problem. I want to learn how to do this, so If anyone does post code, add captions to show what each line does.

-Thanks

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Daphoeno · Mar 05, 2013 at 07:18 PM

Try this out. Be sure that your character has an extra collider on it set to trigger then make sure your enemy has a rigidbody and this should work. If you have any problems I will help you :)

 var PlayerHealth : int = 100;
 var MaxPlayerHealth : int = 100;
 var DeathAnimaton : String;
 var CurrentLevel : String;
         
 function Start ()
 {
     PlayerHealth = MaxPlayerHealth;
 }
         
 function Update ()
 {
     if(PlayerHealth >= MaxPlayerHealth){
         PlayerHealth = MaxPlayerHealth;
     }
         
     if(PlayerHealth <= 0){
         PlayerHealth = 0;
         animation.Play(DeathAnimation);
         Application.LoadLevel(CurrentLevel);
     }
 }
         
 function OnTriggerEnter (other : Collider)
 {
     PlayerHealth = PlayerHealth - 10;
 }
Comment
Add comment · Show 2 · 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 sandhillceltic · Mar 05, 2013 at 08:06 PM 0
Share

I have messed around with the script you gave me, with some luck. I removed the death animation, as my game is in first person, but I encountered a problem- In the last few lines, I noticed a reference to a trigger in the OnTriggerEnter command. It's trying to reference to collision with other objects i'm guessing, but nothing occurs when I collide. How would I bind this collision to another certain mesh? The code is not working either, I tested it. I bound it to the entity that would be receiving the damage (the player), but nothing happened, even when I ran into different meshes numerous times.

function OnTriggerEnter (other : Collider) { PlayerHealth = PlayerHealth - 10; }

Could you give a revision of your code and tell me why it is not working this time?

-Thanks

and sorry about the script not showing up right in the comment, ID$$anonymous$$ what is happening

avatar image Daphoeno · Mar 05, 2013 at 08:14 PM 0
Share

first of all be sure that your enemy has a rigid body on it. If it still doe not work then add a variable for your enemy that is a GameObject then change the OnTriggerEntrance function so it says function OnTriggerEnter (Enemy : Collider) with Enemy being the new variable.

For your other problem your player should have a new collider that is a trigger that is bigger than the player

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

11 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

Related Questions

Problem with health and level reload system 2 Answers

How do I delete an object on collision with another object? 1 Answer

How to attract moving object to specific areas -1 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to do basic health & damage on collision 1 Answer


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