Lives remaining until return to main menu method

Silver100

Member
Hi,

Hope you can help on this. I am trying to see if there is something in Opsive that may help with this issue or integrate into my own code anything that may work like life counter method system.

I have my own health method works really well, just health bar method when depleted returns to main menu after player dies scene gets re-set.

However I have tried to devise a system for 'lives remaining'. i.e 3 Icons displayed, then dies player has 2 lives then 1 live, then player finally dies returns to main menu.
before 'Game Over' message displays back to main menu.

I have experimented with Opsives re-spawner script (which is automatically added to player on Opsive set up). Clicked scheduled respawn on death (matched the respawn to 2 seconds tried various other numbers too), so the code doesn't re-set (rather than load my entire scene on death). If there is a way to keep a count here before re-spawn x 3 then return to main menu?

This works well in the sense the player lives Icons are not going to be re-set (information is retained) and are behaving how I would expect.

For lives Icons I have used Opsive running Icon to represent a life. The issue I'm having though is the death animation gets cancelled and huge delays (20 seconds before main menu). The player stops responding to damage occasionally basically it stops working so well. Is there a better method? Am I on the right tracks here as far as own lives system concerned? I'm not sure if I am approaching this the right way, just a bit guess work trial and error really.

public GameObject Live1;// the image icon I have used Opsive running Icon to represent lives
public GameObject Live2;
public GameObject Live3;

private bool threeLives;// defines what happens under condition threeLives
private bool twoLives;// defines what happens under condition twoLives etc
private bool oneLive;

void OnTriggerEnter(Collider other)//
{
if (playerCurrentHealth < 0 && !isDead) //
// if (other.tag == "Player destroy" && !isDead) // if player is dead 'tag condition' the following function below happen
{
{
var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();//Opsive API method
var dieAbility = characterLocomotion.GetAbility<Die>();
characterLocomotion.TryStartAbility(dieAbility);//Opsive API method

StartCoroutine(delay(v: 30));// set delay function in motion
Gameover.enabled = true;// text to annouce game over

isDead = true;// bool to indicate the above actions represent is dead.i.e less than 0 and is dead the above events happens }
IEnumerator delay(int v) // need to switch off respawn
{

if (playerCurrentHealth < 0 && !isDead & twoLives)
{
Live2.SetActive(false);
oneLive=true;
}

if (playerCurrentHealth < 0 && !isDead & oneLives)
{
yield return new WaitForSeconds(2.0f);//
Live1.SetActive(false);
Gameover.enabled = true;//
isDead = true;
SceneManager.LoadScene(loadLevel);//
Time.timeScale = 0;
}

yield return new WaitForSeconds(2.5f);// This point player spawn back to life 2.5f matching Opsive respawn counter too
isDead = false;// important to bring to life again or conditions under isDead will remain
Live3.SetActive(false);
twoLives = true;
GetComponent<PlayerhealthPR>().HurtPlayer(-1000);//The Player gets health back as script no longer re-loading with new lives method
 
Last edited:
Ok before going too deep, I very highly suggest you study the events and how they are used. for example you could subscribe to "OnDeath" > EventHandler.RegisterEvent<Vector3,Vector3,GameObject>(characterLocomotion.gameObject, "OnDeath" OnDeath);
void OnDeath(Vector3 pos, Vector3 force, GameObject attacker){// do stuff}

As soon as you get comfortable with that, I am sure you'll have no trouble doing whatever you need in UCC. You can also execute your own events. Global event > EventHandler.ExecuteEvent("MyGlobalEvent") or for a Local event > EventHandler.ExecuteEvent(characterLocomotion.gameObject, "MyLocalEvent")... You can also add parameters etc as per OnDeath
 
Thanks Ill take a look at those, I was thinking along the lines of detecting x 3 spawn attempts then just play death anim (as normal would) & return to main menu I have almost got this working but it locks up at the end for 20 seconds so must have conflict somewhere along the lines I may eventually get it working. I may have simply used over complicated method (Booleans) a counter method would be better I think, to keep tab of re-spawn would be simple just a few lines of code. Like the The Helicopter looks a great idea may eventually get that as there is possibly a helicopter piloting scene at then end keeping simple for now though.
 
Last edited:
If you are using respawner component then you likely need it disabled until after your count is up. An easy way to do it would be a custom ability, blocking death if required until count is up (just a thought, not tested but should work), public override bool ShouldBlockAbilityStart(Ability startingAbility) is what youd want to override in your custom ability off hand. Then inside that something like if(count < 3 && startingAbility is Die) return;count++;
 
Thanks for your help on this and giving me some ideas. Definitely needed to pay attention to the respawner when using my method along side and take a look more into what you mentioned. I have got it to work now. I am only learning so this may not be prefect but seems to work really well. I used integer method instead; I couldn't disable the respawner (did try destroy method but couldn't get to work)
but I was able to time it right so events occur before respawn starts over (if timed too late player will come back to life)

I figured making sure the respawner allowed plenty of time for my events to occur (so no change of respawning on final life)
Keeping Opsive health values Min 0, and the rest the same as my health method i.e 1000 & 1000.

public int Livesdeducted = 0;
public GameObject Live1;
public GameObject Live2;
public GameObject Live3;
//set Opsive respawner timer both values to 4
void OnTriggerEnter(Collider other)// Made changes below as new text seems to work different*************************
{
if (playerCurrentHealth < 0 && !isDead) // added health to reverse put back line below***
// if (other.tag == "Player destroy" && !isDead) // if player is dead 'tag condition' the following function below
{
{
var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();//Opsive API ref
var dieAbility = characterLocomotion.GetAbility<Die>();
// Tries to start the jump ability. There are many cases where the ability may not start,
// such as if it doesn't have a high enough priority or if CanStartAbility returns false.
characterLocomotion.TryStartAbility(dieAbility);//Opsive API ref


StartCoroutine(delay(v: 30));// set delay function in motion
Gameover.enabled = true;// text to announce game over

Livesdeducted += 1 * 1;//
isDead = true;// bool to indicate the above actions represent

}

IEnumerator delay(int v) //
{
yield return new WaitForSeconds(2.3f);// after a few seconds (time for animation to play)
GetComponent<Playerhealth>().HurtPlayer(-1000);// script no longer will be resetting as using respawner so health restored here
isDead = false;// not sure if I need this but checking performance with

if (Livesdeducted == 1)
{
Live3.SetActive(false);// when 1st life deducted delete a life image.
isDead = false; // after live deducted confirms alive again and isdead function finished
}
if (Livesdeducted == 2)// when 2nd life deducted
{
Live2.SetActive(false); When 2nd life deducted delete 2nd life image.
isDead = false;
}

if (Livesdeducted == 3)// Game over 3 lives deducted
{
Gameover.enabled = true;// To extend over time would require disabling respawner or seperate game over messgae in new scene
Live1.SetActive(false);
isDead = true;
yield return new WaitForSeconds(0.0f);// can adjust if needed
SceneManager.LoadScene(loadLevel);
}
}

}
}
 
Last edited:
Is there any way to cancel out the Opsives health aspect of the respawner attriblute? If 1 set to 0 can no longer die. If I match my own health setting in own method its fine until a health pick up is collected. Its quite a challenge figuring a way getting player to have a health pick up and update both (when I have never been using Opsive health so no knowledge). At the moment my health pick up will create issues when using the respawner.
 
Sorted had nothing to do with adding lives or respawner. Seems the error was my health method even before I added lives;

I had not set a limit to health. Player was gaining health, so when depleted still had anything over 100%. Needed to set a limit in my code for max health, or to keep simple or just give out smaller amounts
of health so its unlikely to go above 100% Cant believe I have never noticed this.

This method worked;
Max health - Current health = Healthtogive //
Current health - Maxhealth = Healthtogive// or as its a reversing damage - re-writ this way

if (other.tag == "Pickup" && playerCurrentHealth < 1000)

{
Healthtogive method here
}
 
Last edited:
Top