- Home /
Controls stop when player respawns
Hi I am working on a basic respawn which just moves the player when they meet the condition to die. The test cube re-appears as intended but for some reason the controls seem to suddenly stop when the cube re-positions itself to the co-ordinates stated.
Was just wondering if anyone had any ideas looking at my script what is causing this to occur. I am using states as I find them to be very useful in helping me organize my code blocks. Thankful for any help offered.
using UnityEngine; using System.Collections;
public class Player2 : MonoBehaviour { public enum State { Playing, Dyng, } public static State currentState = State.Playing; private Transform Player; public static float playerSpeed; public static float playerStrength; public static bool hasDied = false;
// Use this for initialization
void Start () {
Player = transform;
Player.transform.position = new Vector3 (0, 1, 0);
playerSpeed = 5.0f;
playerStrength = 2.5f;
}
// Update is called once per frame
void Update () {
if (currentState != State.Dyng) {
Player.Translate (Vector3.right * playerSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime);
Player.Translate (Vector3.up * playerSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
}
if (newLiftMechanic.inContact) {//used to check if the player is touching an object that can be lifted
playerSpeed = 0.0f;
} else {
playerSpeed = 5.0f;
}
if (hasDied) {
StartCoroutine(Died());
}
}
IEnumerator Died(){
currentState = State.Dyng;
renderer.enabled = false;
Player.transform.position = new Vector3 (0, 1, 0);
yield return new WaitForSeconds (0.5f);
currentState = State.Playing;
renderer.enabled = true;
}
}
Answer by Zentiu · Mar 10, 2014 at 06:17 PM
hmm..
try resetting your playerSpeed in your IEnumerator somewhere like:
currentState = State.Playing;
playerSpeed = 5.0f;
renderer.enabled = true;
see if that solves the problem.
also turn off the coroutine with:
hadDied = false;
StopCoroutine(died());
in your IEnumerator just after you enable the renderer.
hope this helps.
Thank you. This worked but the StopCoroutine gave me this error - error CS1502: The best overloaded method match for `UnityEngine.$$anonymous$$onoBehaviour.StopCoroutine(string)' has some invalid arguments, but ultimately this has helped!!
Thank you so much for your assistance.
my bad.. I copied it.. it should've been StopCoroutine("Died");
at least my answer was partially useful :P. glad it worked.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135913im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Respawn question 2 Answers
How do I make one transform rotation the same as anothers? 2 Answers
Getting an object to re-spawn/ transform player back to the beginning 1 Answer
Objects with same position appear in different places 2 Answers
Offset against Rotated Object,Applying Offset to a Rotated Transform 1 Answer