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
-1
Question by Djspun · Sep 30, 2013 at 02:15 PM · transformpositionplayerhealth

c# how to change player location on 0 hp

i'm looking to change the player location from anywhere on 0 PlayerHealth

i have a specific location i want the player to end up when PlayerHealth is 0 and is it possible to add this to the player health script or should it be its own script accessing it

for if im to add to this under if(curHealth <= 0) the ??? is the answer i couldn't find

PlayerHealth

/// /// PlayerHealth.cs /// Display the players health in game /// /// Attach this class to your player character /// using UnityEngine; using System.Collections;

public class PlayerHealth : MonoBehaviour { public int maxHealth = 100; public int curHealth = 100;

 public float healthBarLength;

 // Use this for initialization
 void Start () {
     healthBarLength = Screen.width / 2;
 }
 
 // Update is called once per frame
 void Update () {
     AddjustCurrentHealth(0);
 }
 
 void OnGUI() {
     GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
 }
 
 public void AddjustCurrentHealth(int adj) {
     curHealth += adj;
     
     if(curHealth < 0)
         curHealth = 0;
     
     if(curHealth > maxHealth)
         curHealth = maxHealth;
     
     if(maxHealth < 1)
         maxHealth = 1;
     
     if(curHealth <= 0)

{

?

}

healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);

}

}

Comment
Add comment · Show 2
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 mattssonon · Sep 30, 2013 at 02:19 PM 0
Share

Please format your code properly.

avatar image vexe · Oct 06, 2013 at 09:39 AM 0
Share

@Djspun: Is there something else we can do for you? If not, then please tick an answer it helped, if you have other unrelated questions, please ask them separately. Thanks.

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by vexe · Sep 30, 2013 at 02:24 PM

It's better to keep your components separate, a health script manages your player's health, a shooting script is for shooting, etc. (If you wanna go component-based design, and not OOP where everything related to your player, is in your player's script/class)

That job (what you're trying to do) sounds to me is suitable to fit inside a GameManager or GameController script, which should be responsible for what happens when your player dies, how do you earn score, deaths, kills, etc. (Much like the GameController in UDK) - I advice you to create that script and put such things in it.

That Manager/Controller could be a singleton. Then, you have multiple ways to inform the manager, about the death of your player (reaching 0 health)

1- Create a public void Notify_PlayerDead() method inside your controller, that you call when the player dies, do your stuff inside it.

2- A more professional way would be to use events, create an OnPlayerDeath event and have -whoever needs to be notified about this event- subscribe to it. See events and observer pattern. (Link1, Link2, Link3)

Comment
Add comment · Show 10 · 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 Djspun · Sep 30, 2013 at 02:27 PM 0
Share

its a single player rpg no need to keep score on deaths i just want the location change on 0 hp sounds good and all but ill just stick with what im doing im still newish

avatar image Djspun · Sep 30, 2013 at 02:28 PM 0
Share

if im to do it in another script how and this way i could add other things eventual

avatar image vexe · Sep 30, 2013 at 02:40 PM 1
Share

I also highly recommend getting stronger at program$$anonymous$$g first, then hopping over to Unity. I solve most if not all my problems not by my 'Unity' knowledge (which is currently very humble) but by logical thinking which only comes from getting better and better at program$$anonymous$$g.

avatar image vexe · Sep 30, 2013 at 02:42 PM 1
Share

"i dont think i want to count player deaths in game"

I just gave you an example of what could be included in the Game$$anonymous$$anager/Controller, it's up to you and your game.

avatar image vexe · Oct 01, 2013 at 05:04 AM 2
Share

I think it is better to write another separate well-formatted and written question for that.

Show more comments

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

18 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

Related Questions

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

Position of a GameObject 2 Answers

Need enemy to follow only active character 0 Answers

Checking Position 1 Answer

Game Object won't match rotation of new positions transform 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