- Home /
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);
}
}
@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.
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)
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
if im to do it in another script how and this way i could add other things eventual
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.
"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.
I think it is better to write another separate well-formatted and written question for that.
Your answer
Follow this Question
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