Make player use fixed amount of health?












1















Hey I want to make my player invicible, when he takes damage and loose a health. ATM I cant make my player invicible, and also if he collides with several damage structures he takes more damage than intended I want him to lose 20% of his health and then be invincible for 2-3 sec.



This is attached to my damage structure



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoseHealth : MonoBehaviour {



private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}

}
}


This is for my FullHearts on the canvas attached as a child to my "empty hearts"



  using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class HealthBarScript : MonoBehaviour {


Image FullHearts;
float maxHealth = 100f;
public static float health;
// Use this for initialization
void Start () {
FullHearts = GetComponent<Image>();
health = maxHealth;
}

// Update is called once per frame
void Update () {
FullHearts.fillAmount = health / maxHealth;
if (health == 0)
{
Application.LoadLevel(Application.loadedLevel);
}
}
}


ATM I have this on my playerscript which is not working all other scripts are working but just need it to only be 1 heart to lose no matter if the player hits multiple structures and then invicible to have time to recover and not hit the structure :



  if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
new WaitForSeconds(3);
invincible = false; // makes this whole function reusable since invincible is false again
}


}









share|improve this question























  • Just a wild guess, should it be "yield return new WaitForSeconds(3);"?

    – rs232
    Nov 15 '18 at 11:54











  • But then i need a coroutine and since it is only a bool i have to make it do something so the player cant be damaged somehow but i guess since i make the function not usable it will be invicible or? So maybe ill try a coroutine :D

    – noobprogrammer1337
    Nov 15 '18 at 11:56













  • How it is now, it will do the following: 1) make the player invincible. 2) immediately create an object which represents (just represents, it does not do anything on its own!) waiting for three seconds. 3) immediately make the player able to receive damage again. 4) proceed with other tasks

    – rs232
    Nov 15 '18 at 12:15
















1















Hey I want to make my player invicible, when he takes damage and loose a health. ATM I cant make my player invicible, and also if he collides with several damage structures he takes more damage than intended I want him to lose 20% of his health and then be invincible for 2-3 sec.



This is attached to my damage structure



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoseHealth : MonoBehaviour {



private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}

}
}


This is for my FullHearts on the canvas attached as a child to my "empty hearts"



  using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class HealthBarScript : MonoBehaviour {


Image FullHearts;
float maxHealth = 100f;
public static float health;
// Use this for initialization
void Start () {
FullHearts = GetComponent<Image>();
health = maxHealth;
}

// Update is called once per frame
void Update () {
FullHearts.fillAmount = health / maxHealth;
if (health == 0)
{
Application.LoadLevel(Application.loadedLevel);
}
}
}


ATM I have this on my playerscript which is not working all other scripts are working but just need it to only be 1 heart to lose no matter if the player hits multiple structures and then invicible to have time to recover and not hit the structure :



  if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
new WaitForSeconds(3);
invincible = false; // makes this whole function reusable since invincible is false again
}


}









share|improve this question























  • Just a wild guess, should it be "yield return new WaitForSeconds(3);"?

    – rs232
    Nov 15 '18 at 11:54











  • But then i need a coroutine and since it is only a bool i have to make it do something so the player cant be damaged somehow but i guess since i make the function not usable it will be invicible or? So maybe ill try a coroutine :D

    – noobprogrammer1337
    Nov 15 '18 at 11:56













  • How it is now, it will do the following: 1) make the player invincible. 2) immediately create an object which represents (just represents, it does not do anything on its own!) waiting for three seconds. 3) immediately make the player able to receive damage again. 4) proceed with other tasks

    – rs232
    Nov 15 '18 at 12:15














1












1








1








Hey I want to make my player invicible, when he takes damage and loose a health. ATM I cant make my player invicible, and also if he collides with several damage structures he takes more damage than intended I want him to lose 20% of his health and then be invincible for 2-3 sec.



This is attached to my damage structure



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoseHealth : MonoBehaviour {



private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}

}
}


This is for my FullHearts on the canvas attached as a child to my "empty hearts"



  using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class HealthBarScript : MonoBehaviour {


Image FullHearts;
float maxHealth = 100f;
public static float health;
// Use this for initialization
void Start () {
FullHearts = GetComponent<Image>();
health = maxHealth;
}

// Update is called once per frame
void Update () {
FullHearts.fillAmount = health / maxHealth;
if (health == 0)
{
Application.LoadLevel(Application.loadedLevel);
}
}
}


ATM I have this on my playerscript which is not working all other scripts are working but just need it to only be 1 heart to lose no matter if the player hits multiple structures and then invicible to have time to recover and not hit the structure :



  if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
new WaitForSeconds(3);
invincible = false; // makes this whole function reusable since invincible is false again
}


}









share|improve this question














Hey I want to make my player invicible, when he takes damage and loose a health. ATM I cant make my player invicible, and also if he collides with several damage structures he takes more damage than intended I want him to lose 20% of his health and then be invincible for 2-3 sec.



This is attached to my damage structure



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoseHealth : MonoBehaviour {



private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}

}
}


This is for my FullHearts on the canvas attached as a child to my "empty hearts"



  using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class HealthBarScript : MonoBehaviour {


Image FullHearts;
float maxHealth = 100f;
public static float health;
// Use this for initialization
void Start () {
FullHearts = GetComponent<Image>();
health = maxHealth;
}

// Update is called once per frame
void Update () {
FullHearts.fillAmount = health / maxHealth;
if (health == 0)
{
Application.LoadLevel(Application.loadedLevel);
}
}
}


ATM I have this on my playerscript which is not working all other scripts are working but just need it to only be 1 heart to lose no matter if the player hits multiple structures and then invicible to have time to recover and not hit the structure :



  if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
new WaitForSeconds(3);
invincible = false; // makes this whole function reusable since invincible is false again
}


}






c# unity3d 2d






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 11:50









noobprogrammer1337noobprogrammer1337

327




327













  • Just a wild guess, should it be "yield return new WaitForSeconds(3);"?

    – rs232
    Nov 15 '18 at 11:54











  • But then i need a coroutine and since it is only a bool i have to make it do something so the player cant be damaged somehow but i guess since i make the function not usable it will be invicible or? So maybe ill try a coroutine :D

    – noobprogrammer1337
    Nov 15 '18 at 11:56













  • How it is now, it will do the following: 1) make the player invincible. 2) immediately create an object which represents (just represents, it does not do anything on its own!) waiting for three seconds. 3) immediately make the player able to receive damage again. 4) proceed with other tasks

    – rs232
    Nov 15 '18 at 12:15



















  • Just a wild guess, should it be "yield return new WaitForSeconds(3);"?

    – rs232
    Nov 15 '18 at 11:54











  • But then i need a coroutine and since it is only a bool i have to make it do something so the player cant be damaged somehow but i guess since i make the function not usable it will be invicible or? So maybe ill try a coroutine :D

    – noobprogrammer1337
    Nov 15 '18 at 11:56













  • How it is now, it will do the following: 1) make the player invincible. 2) immediately create an object which represents (just represents, it does not do anything on its own!) waiting for three seconds. 3) immediately make the player able to receive damage again. 4) proceed with other tasks

    – rs232
    Nov 15 '18 at 12:15

















Just a wild guess, should it be "yield return new WaitForSeconds(3);"?

– rs232
Nov 15 '18 at 11:54





Just a wild guess, should it be "yield return new WaitForSeconds(3);"?

– rs232
Nov 15 '18 at 11:54













But then i need a coroutine and since it is only a bool i have to make it do something so the player cant be damaged somehow but i guess since i make the function not usable it will be invicible or? So maybe ill try a coroutine :D

– noobprogrammer1337
Nov 15 '18 at 11:56







But then i need a coroutine and since it is only a bool i have to make it do something so the player cant be damaged somehow but i guess since i make the function not usable it will be invicible or? So maybe ill try a coroutine :D

– noobprogrammer1337
Nov 15 '18 at 11:56















How it is now, it will do the following: 1) make the player invincible. 2) immediately create an object which represents (just represents, it does not do anything on its own!) waiting for three seconds. 3) immediately make the player able to receive damage again. 4) proceed with other tasks

– rs232
Nov 15 '18 at 12:15





How it is now, it will do the following: 1) make the player invincible. 2) immediately create an object which represents (just represents, it does not do anything on its own!) waiting for three seconds. 3) immediately make the player able to receive damage again. 4) proceed with other tasks

– rs232
Nov 15 '18 at 12:15












1 Answer
1






active

oldest

votes


















1














So I just made a simple collision 'game' using the method you provided and everything works fine. But now as I look further, I am seeing some redundancy in what you provided and your 'answer' is a bit confusing.



But your first script shows that every time the damage dealer collides with 'Player' it does damage.



private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}

}


AND every time the 'Player' collides with an 'enemy' it does an invincible check which should be working. (Assuming the comments are removed). But as the comments stand there is no damage from the 'Player' hitting the damage structure but always (regardless of invincible) damage from the structure to the player.



if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
StartCoroutine(Invincible()); // makes this whole function reusable since invincible is false again
}


}
}


IEnumerator Invincible()
{
if (invincible == true)


{
yield return new WaitForSeconds(2);
{


invincible = false;
}
}


But when you hit again before the invincible check expires the script on the enemy is still dealing damage.



You have two scripts doing damage and only one does an invincible check.



Get rid of the damaging script on the structures, tag all the structures properly, comment the script on the player and you will be good to go. Or you can keep the script on the damage structures and just have each one check if the player is invincible by making that bool static.



Also you could have easily answered your own question with standard usage of Debug.Log("what the heck is happening"). Debug.Log should be your best friend.






share|improve this answer
























  • okay i will try but how would u go about only getting 20% of the health taken and not 60% if it hits 3 objects?

    – noobprogrammer1337
    Nov 15 '18 at 14:59











  • I made it work had tagged enemy when i needed Enemy but now i just need to be sure it can only hit for 20% = 1 heart he has 5 hearts displayed so it takes them out on each life he looses :D

    – noobprogrammer1337
    Nov 15 '18 at 15:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318861%2fmake-player-use-fixed-amount-of-health%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














So I just made a simple collision 'game' using the method you provided and everything works fine. But now as I look further, I am seeing some redundancy in what you provided and your 'answer' is a bit confusing.



But your first script shows that every time the damage dealer collides with 'Player' it does damage.



private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}

}


AND every time the 'Player' collides with an 'enemy' it does an invincible check which should be working. (Assuming the comments are removed). But as the comments stand there is no damage from the 'Player' hitting the damage structure but always (regardless of invincible) damage from the structure to the player.



if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
StartCoroutine(Invincible()); // makes this whole function reusable since invincible is false again
}


}
}


IEnumerator Invincible()
{
if (invincible == true)


{
yield return new WaitForSeconds(2);
{


invincible = false;
}
}


But when you hit again before the invincible check expires the script on the enemy is still dealing damage.



You have two scripts doing damage and only one does an invincible check.



Get rid of the damaging script on the structures, tag all the structures properly, comment the script on the player and you will be good to go. Or you can keep the script on the damage structures and just have each one check if the player is invincible by making that bool static.



Also you could have easily answered your own question with standard usage of Debug.Log("what the heck is happening"). Debug.Log should be your best friend.






share|improve this answer
























  • okay i will try but how would u go about only getting 20% of the health taken and not 60% if it hits 3 objects?

    – noobprogrammer1337
    Nov 15 '18 at 14:59











  • I made it work had tagged enemy when i needed Enemy but now i just need to be sure it can only hit for 20% = 1 heart he has 5 hearts displayed so it takes them out on each life he looses :D

    – noobprogrammer1337
    Nov 15 '18 at 15:19
















1














So I just made a simple collision 'game' using the method you provided and everything works fine. But now as I look further, I am seeing some redundancy in what you provided and your 'answer' is a bit confusing.



But your first script shows that every time the damage dealer collides with 'Player' it does damage.



private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}

}


AND every time the 'Player' collides with an 'enemy' it does an invincible check which should be working. (Assuming the comments are removed). But as the comments stand there is no damage from the 'Player' hitting the damage structure but always (regardless of invincible) damage from the structure to the player.



if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
StartCoroutine(Invincible()); // makes this whole function reusable since invincible is false again
}


}
}


IEnumerator Invincible()
{
if (invincible == true)


{
yield return new WaitForSeconds(2);
{


invincible = false;
}
}


But when you hit again before the invincible check expires the script on the enemy is still dealing damage.



You have two scripts doing damage and only one does an invincible check.



Get rid of the damaging script on the structures, tag all the structures properly, comment the script on the player and you will be good to go. Or you can keep the script on the damage structures and just have each one check if the player is invincible by making that bool static.



Also you could have easily answered your own question with standard usage of Debug.Log("what the heck is happening"). Debug.Log should be your best friend.






share|improve this answer
























  • okay i will try but how would u go about only getting 20% of the health taken and not 60% if it hits 3 objects?

    – noobprogrammer1337
    Nov 15 '18 at 14:59











  • I made it work had tagged enemy when i needed Enemy but now i just need to be sure it can only hit for 20% = 1 heart he has 5 hearts displayed so it takes them out on each life he looses :D

    – noobprogrammer1337
    Nov 15 '18 at 15:19














1












1








1







So I just made a simple collision 'game' using the method you provided and everything works fine. But now as I look further, I am seeing some redundancy in what you provided and your 'answer' is a bit confusing.



But your first script shows that every time the damage dealer collides with 'Player' it does damage.



private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}

}


AND every time the 'Player' collides with an 'enemy' it does an invincible check which should be working. (Assuming the comments are removed). But as the comments stand there is no damage from the 'Player' hitting the damage structure but always (regardless of invincible) damage from the structure to the player.



if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
StartCoroutine(Invincible()); // makes this whole function reusable since invincible is false again
}


}
}


IEnumerator Invincible()
{
if (invincible == true)


{
yield return new WaitForSeconds(2);
{


invincible = false;
}
}


But when you hit again before the invincible check expires the script on the enemy is still dealing damage.



You have two scripts doing damage and only one does an invincible check.



Get rid of the damaging script on the structures, tag all the structures properly, comment the script on the player and you will be good to go. Or you can keep the script on the damage structures and just have each one check if the player is invincible by making that bool static.



Also you could have easily answered your own question with standard usage of Debug.Log("what the heck is happening"). Debug.Log should be your best friend.






share|improve this answer













So I just made a simple collision 'game' using the method you provided and everything works fine. But now as I look further, I am seeing some redundancy in what you provided and your 'answer' is a bit confusing.



But your first script shows that every time the damage dealer collides with 'Player' it does damage.



private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}

}


AND every time the 'Player' collides with an 'enemy' it does an invincible check which should be working. (Assuming the comments are removed). But as the comments stand there is no damage from the 'Player' hitting the damage structure but always (regardless of invincible) damage from the structure to the player.



if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
StartCoroutine(Invincible()); // makes this whole function reusable since invincible is false again
}


}
}


IEnumerator Invincible()
{
if (invincible == true)


{
yield return new WaitForSeconds(2);
{


invincible = false;
}
}


But when you hit again before the invincible check expires the script on the enemy is still dealing damage.



You have two scripts doing damage and only one does an invincible check.



Get rid of the damaging script on the structures, tag all the structures properly, comment the script on the player and you will be good to go. Or you can keep the script on the damage structures and just have each one check if the player is invincible by making that bool static.



Also you could have easily answered your own question with standard usage of Debug.Log("what the heck is happening"). Debug.Log should be your best friend.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 13:47









Brian GreenBrian Green

412




412













  • okay i will try but how would u go about only getting 20% of the health taken and not 60% if it hits 3 objects?

    – noobprogrammer1337
    Nov 15 '18 at 14:59











  • I made it work had tagged enemy when i needed Enemy but now i just need to be sure it can only hit for 20% = 1 heart he has 5 hearts displayed so it takes them out on each life he looses :D

    – noobprogrammer1337
    Nov 15 '18 at 15:19



















  • okay i will try but how would u go about only getting 20% of the health taken and not 60% if it hits 3 objects?

    – noobprogrammer1337
    Nov 15 '18 at 14:59











  • I made it work had tagged enemy when i needed Enemy but now i just need to be sure it can only hit for 20% = 1 heart he has 5 hearts displayed so it takes them out on each life he looses :D

    – noobprogrammer1337
    Nov 15 '18 at 15:19

















okay i will try but how would u go about only getting 20% of the health taken and not 60% if it hits 3 objects?

– noobprogrammer1337
Nov 15 '18 at 14:59





okay i will try but how would u go about only getting 20% of the health taken and not 60% if it hits 3 objects?

– noobprogrammer1337
Nov 15 '18 at 14:59













I made it work had tagged enemy when i needed Enemy but now i just need to be sure it can only hit for 20% = 1 heart he has 5 hearts displayed so it takes them out on each life he looses :D

– noobprogrammer1337
Nov 15 '18 at 15:19





I made it work had tagged enemy when i needed Enemy but now i just need to be sure it can only hit for 20% = 1 heart he has 5 hearts displayed so it takes them out on each life he looses :D

– noobprogrammer1337
Nov 15 '18 at 15:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318861%2fmake-player-use-fixed-amount-of-health%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()