C# Help - Respawn or Reset

I was wondering if anyone could just lend me a moment of there time to help me with a simple problem.

I’ve being using the Free Fantasy AI that you can download from the unity asset store, within the game package there is a health script that I have being working on. Within the health script I was able to add a destroy script that would destroy the player gameobject once his/her health is gone.

However the problem I’m having is that I wish to have either the player respawn back at his/her spawn point OR either have the level completely restart?

If anyone could give me some help that would be great. Just in case anyone is wondering that script below is the health script that from the Free Fantasy AI…
public class Health : MonoBehaviour {
public float MaxHealth=100;
public float CurrentHealth;
public bool Invincible;
public bool Dead;

	// Use this for initialization
	void Start () {
		//MAKE THE CURRENT HEALTH THE MAX HEALTH AT START
	CurrentHealth=MaxHealth;
	}
	
	// Update is called once per frame
	void Update () {
	
		//IF INVINCIBLE, HE CANNOT DIE..
		if(Invincible){
		CurrentHealth=MaxHealth;	
		}
		else{
		if(CurrentHealth<=0){
			CurrentHealth=0;
			Dead=true;
		}	
			
		//MAX HEALTH
			if(CurrentHealth>=MaxHealth)CurrentHealth=MaxHealth;
			
			//WHEN DEATH IS UPON HIM
		if(Dead){
				Destroy(gameObject);
				//TELL THE AI SCRIPT HE IS DEAD
			FreeAI AI=(FreeAI)GetComponent("FreeAI");
				if(AI){
			if(AI.IsDead){}
			else AI.IsDead=true;
		}
		}
		}
	
	}
}

Go dtí an chéad uair eile, slán leat

Saying “ForceMode.Impulse == Force per frame” is totally wrong. The author even realized it by himself by saying "It will only be applied at the instant you call the function". ---------- I am tired to explain this again, Unity physics is bad physics because those Forcemodes were not correctly implemented as it sounds. ---------- Basically, they are all the same method, two of them have 50x speed(50 physics frame in a second) multiplier, two of them include Mass.

1 Answer

1

Hi

To the do the respawn, you could add some more class variables to store it’s starting location and rotation. Save them in your Start method and then once the character dies, maybe wait a set duration, reset their stats like HP to full and then move them to the saved start position and rotation. So you dont need to destroy the gameobject.
With reusing the game object you gotta make sure everything attributed to that character is completely reset too!

For a total restart of the level? A bit more complicated, have a script to ‘manage’ the level which keeps track of how many characters are dead and if they all are, then just do above and reset everyone or instantiate brand new versions of them in new places, move the player back to a starting location and if everything needed to set up a level is reset it should work no problem.

When I started doing levels the cut was a bit abrupt so I used a FadeOut script from the wiki to get a nicer transition. Good luck!

Except that it perpetuates the confusing wording of the docs, and actually makes it even worse. Jeff says The force is applied evenly across the period of the frame but that's simply not true. It does not apply the force across the "period of the frame" (whatever that means), it applies it instantly, exactly the same as .Impulse does. The difference is that it assumes the force you're inputting is force per second rather than force per frame.