- Home /
With my respawn script, Player can't move for about 5 seconds.
(Tried asking this combined with another question, got declined by moderators because it is too basic apparently, so I am posting this one here, and I have searched on this, so don't decline it again please...)
The code I use to make the player die and respawn works nearly perfectly, except for the fact that after he respawns, he can only turn left and right (A & D keys), and he cannot walk backward or forward. This happens for about five seconds after the respawn, and after position is set to 0,0,0. Can anyone help me with this? Thanks in advance. (New to Unity/C#/Programming/Game Development)
Here is the code:
using UnityEngine;
using System.Collections;
public class AttributeDeclaration : MonoBehaviour {
#region Public ints
public int Health = 100;
public int Magic = 100;
public int Strength = 100;
public int Stamina = 100;
public int Defense = 100;
//Maximum attributes
public int MaxHealth = 100;
public int MaxMagic = 100;
public int MaxStrength = 100;
public int MaxStamina = 100;
public int MaxDefense = 100;
//Testing Stuff
public int DamageAmount = 20;
//---------------------------------------------------------------------------------------------
#endregion
public PlayerLevelDeclaration PT;
public PlayerMovement PM;
public HealthPotion HP;
public GUIs GU;
//----------------------------------------------------------------------------------------
void Awake(){
newPos = transform.position;
//newRot = transform.rotation;
}
//-------------------------
// Use this for initialization
void Start () {
}
//-----------------------------------------------------------------------------------------
// Update is called once per frame
void Update () {
PT = gameObject.GetComponent<PlayerLevelDeclaration>();
HP = gameObject.GetComponent<HealthPotion>();
PM = gameObject.GetComponent<PlayerMovement>();
GU = gameObject.GetComponent<GUIs>();
TempDamageHealth();
TempDamageStamina();
TempDamageMagic();
TempDamageStrength();
TempDamageDefense();
DisallowNegative();
//KillPlayer();
}
//----------------------------------------------------------------------------------------
void DisallowNegative(){
if(Health <= 0)
{
Health = 0;
StartCoroutine(KillPlayer()); // Or you can also use StartCoroutine("KillPlayer");
}
if(Health <= 0)
{
Health = 0;
}
if(Stamina <= 0)
{
Stamina = 0;
}
if(Magic <= 0)
{
Magic = 0;
}
if(Strength <= 0)
{
Strength = 0;
}
if(Defense <=0)
{
Defense = 0;
}
}
//------------------------------------------------------------------------------------------
//--------------------------------GUI Crap for player death---------------------------------
//------------------------------------------------------------------------------------------
void OnGUI(){
ShowDeath();
SorryMessage();
}
//-----------------------------------------------------------------------------------------
void ShowDeath(){
if(Health <= 0)
{
//LevelString = PT.PlayerLevel.ToString ();
GUI.Box(new Rect(Screen.width / 2 - 250, Screen.height / 2 - 200, 500, 400), ("Oh dear! It appears you have died! Please wait 5 seconds to respawn"));
}
}
//------------------------------------------------------------------------------------------
void SorryMessage(){
if(Health <= 0){
GUI.Box(new Rect(Screen.width / 2 - 230, Screen.height / 2 - 170, 500, 370), ("Very sorry, may take a few extra seconds, working out some bugs."));
}
}
//------------------------------------------------------------------------------------------
//--------------------------------Temp Damage Attributes------------------------------------
//------------------------------------------------------------------------------------------
public int TempDamageHealth()
{
if(Input.GetKeyDown(KeyCode.Keypad1))
{
Health = Health - DamageAmount;
}
return Health;
}
//-------------------------------------------------------------------------------------
public int TempDamageStamina()
{
if(Input.GetKeyDown(KeyCode.Keypad2))
{
Stamina = Stamina - DamageAmount;
}
return Stamina;
}
//--------------------------------------------------------------------------------------
public int TempDamageMagic()
{
if(Input.GetKeyDown(KeyCode.Keypad3))
{
Magic = Magic - DamageAmount;
}
return Magic;
}
//------------------------------------------------------------------------------------------
public int TempDamageStrength()
{
if(Input.GetKeyDown(KeyCode.Keypad4))
{
Strength = Strength - DamageAmount;
}
return Strength;
}
//----------------------------------------------------------------------------------
public int TempDamageDefense()
{
if(Input.GetKeyDown(KeyCode.Keypad9))
{
Defense = Defense - DamageAmount;
}
return Defense;
}
//--------------------------------------------------------------------
Vector3 newPos;
Vector3 newRot;
IEnumerator KillPlayer(){
if(Health <= 0)
{
Health = 0;
//Hopefully this makes it so the player can't move as soon as Health = 0, but doesn't disappear and lose
//collision until after 2 seconds. Then, 5 seconds later, the player's position is set to 0, 0, 0, and
//He regains movement, reappears, and regains collision.
//Kill Player
PM.enabled = false;
GU.enabled = false;
yield return new WaitForSeconds(2f);
renderer.enabled = false;
collider.enabled = false;
PM.enabled = false;
//Wait 5 seconds
yield return new WaitForSeconds(5f);
PM.enabled = true;
Vector3 PlayerSpawn = new Vector3(0f , 0f , 0f);
if(transform.position != PlayerSpawn){
transform.position = PlayerSpawn;
}
renderer.enabled = true;
collider.enabled = true;
PM.enabled = true;
GU.enabled = true;
//Set position code;
//Rest
Health = MaxHealth;
}
}
}
//Wait 5 seconds
yield return new WaitForSeconds(5f);
P$$anonymous$$.enabled = true;
What's P$$anonymous$$? Does that enable your movement?
Yes, P$$anonymous$$ is my Player$$anonymous$$ovement script That enabled my movement, correct.
But I don't want the player to be able to move before they are shown again or "Respawn"
Did you make sure that you only have one instance of the $$anonymous$$illPlayer() coroutine running at a time like I said in your last question? If you call StartCoroutine in Update you have to be careful(I suggest using the technique in aldonaletto's answer on that page). I notice that $$anonymous$$illPlayer() sets health at 0 at the beginning. $$anonymous$$ake sure it isn't being started more than once at a time.
You have 2 yield WaitForSeconds in the same coroutine. You will get 2 pauses each time the routine is run.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Respawn Script Problem 1 Answer
How to undo destroying a component, after 5 seconds. 0 Answers
Distribute terrain in zones 3 Answers