- Home /
How do you stop player from moving when it dies?
I watched this video about particle effects when the player dies
The problem is when my player dies it disappears but it is still moving and collecting coins. How do I stop this from happening?
public class PlayerLevelManager : MonoBehaviour {
private GameMaster gm;
private PlayerController player;
public GameObject currCheckpoint;
public GameObject deathParticle;
public GameObject spawnParticle;
void Start () {
player = GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerController> ();
gm = GameObject.FindGameObjectWithTag ("GameMaster").GetComponent<GameMaster> ();
gm.health = 5;
gm.ammo = 2;
Instantiate (spawnParticle, player.transform.position, player.transform.rotation);
}
public void Die () {
StartCoroutine ("Respawn");
}
public IEnumerator Respawn () {
gm.health -= 1;
Instantiate (deathParticle, transform.position, transform.rotation);
player.enabled = false;
player.GetComponent<Renderer> ().enabled = false;
yield return new WaitForSeconds (1);
if (gm.health <= 0) {
SceneManager.LoadScene (1);
Instantiate (spawnParticle, currCheckpoint.transform.position, currCheckpoint.transform.rotation);
}
player.transform.position = currCheckpoint.transform.position;
player.enabled = true;
player.GetComponent<Renderer> ().enabled = true;
Instantiate (spawnParticle, currCheckpoint.transform.position, currCheckpoint.transform.rotation);
}
}
Check if player is dead in your Player Controller and disable movement.
Answer by Veftaxop · May 03, 2018 at 03:33 PM
This is your problem here:
player = GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerController> ();
Instead of caching the GameObject you are caching the script PlayerController. You should change it to:
player = GameObject.FindGameObjectWithTag ("Player");
Keep in mind that this will change the variable player from PlayerController to GameObject. It will likely mean some conflicts in other areas of your code.
Also, you are not activating and deactivating properly. Instead of
player.enabled = false;
player.GetComponent<Renderer> ().enabled = false;
use
player.SetActive(false);
to deactivate. To activate is self explainable. By deactivating the Renderer you are just making something invisible and not deactivating it. player.enabled is deprecated use SetActive(bool).
Or simply replace
player.enabled = false;
player.GetComponent<Renderer> ().enabled = false;
By:
player.gameObject.SetActive(false);
Your answer
Follow this Question
Related Questions
Moving and rolling a cube 1 Answer
Player movement 1 Answer
player not moving over the bridge 1 Answer
Non slippery movement 1 Answer
Dash in movement direction, not in forward direction 0 Answers