Search Unity

Player still moves when dead, can cause double respawn

Discussion in 'Scripting' started by JoeCullum, Dec 27, 2016.

  1. JoeCullum

    JoeCullum

    Joined:
    Dec 27, 2016
    Posts:
    18
    So following the gamesplusjames tutorial I've encountered an error where if I hit some spikes and I die, I can't move my character, he keeps moving on his own as if my input is still entered or the gravity is still in function. This causes me to die twice if I touch some spikes, keep falling and then get killed during my respawn delay when he encounters the blast zone. (blast zone being the zone that respawns him when he falls off the stage.)

    I've tried making his position upon death change instantly to the checkpoint position, this makes his death sound the same volume as when he reaches the blast zone or gets killed by an enemy interestingly enough (Another problem which I encountered but doing this solved it). But I still have the same problem. I've tried disabling the PlayerController script when he dies but this seems to make him die every frame he moves after death, which is horrifying. I'm stuck now so any advice would be wonderful. I'll post my PlayerController and HealthManager Script as I think these both would be helpful. Thank you again for reading :)

    PlayerController:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerController : MonoBehaviour {

    public float moveSpeed;
    public float jumpHeight;

    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask whatIsGround;
    public bool grounded;
    private bool doubleJump;
    private Animator anim;
    private float playersx;
    private float moveVelocity;
    public Rigidbody2D rb2d;
    public Transform firePoint;
    public GameObject ninjaStar;
    public float shotDelay;
    public float shotDelayCounter;


    // Use this for initialization
    void Start() {
    anim = GetComponent<Animator>();
    playersx = transform.localScale.x;
    rb2d = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate() {

    grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
    }

    // Update is called once per frame
    void Update()
    {
    moveVelocity = 0f;

    if (grounded)

    doubleJump = false;

    anim.SetBool("Grounded", grounded);

    if (Input.GetKeyDown(KeyCode.W) && grounded)

    {Jump();}

    if (Input.GetKeyDown(KeyCode.W) && !doubleJump && !grounded)

    {DoubleJump();
    doubleJump = true;}

    if (Input.GetKey(KeyCode.D))
    moveVelocity = moveSpeed;
    if (Input.GetKey(KeyCode.A))
    moveVelocity = -moveSpeed;

    if (Input.GetKey(KeyCode.S) & !grounded) {fastFall();}
    if (!grounded & (!Input.GetKey(KeyCode.S))) {fastFallCancel();}





    rb2d.velocity = new Vector2(moveVelocity, rb2d.velocity.y);

    anim.SetFloat ("Speed", Mathf.Abs (rb2d.velocity.x));

    if (rb2d.velocity.x > 0)
    transform.localScale = new Vector3(playersx, transform.localScale.y, transform.localScale.z);
    else if (rb2d.velocity.x < 0)
    transform.localScale = new Vector3(-playersx, transform.localScale.y, transform.localScale.z);

    if (Input.GetKeyDown(KeyCode.Return))
    {
    Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
    shotDelayCounter = shotDelay;
    }

    if (Input.GetKey(KeyCode.Return))
    {
    shotDelayCounter -= Time.deltaTime;

    if (shotDelayCounter <= 0)
    {
    shotDelayCounter = shotDelay;
    Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
    }
    }

    }




    public void Jump()
    {
    rb2d.velocity = new Vector2(rb2d.velocity.x, jumpHeight);
    }
    public void DoubleJump()
    {
    rb2d.velocity = new Vector2(rb2d.velocity.x * 2, jumpHeight * 1.75f);
    }
    public void Right()
    {
    rb2d.velocity = new Vector2(moveSpeed, rb2d.velocity.y);
    }
    public void Left()
    {
    rb2d.velocity = new Vector2(-moveSpeed, rb2d.velocity.y);
    }
    public void fastFall()
    {

    rb2d.gravityScale = 12;
    }
    public void fastFallCancel()
    {
    rb2d.gravityScale = 8;
    }
    }


    HealthManager script:

    using System.Collections;
    using UnityEngine.UI;
    using System.Collections.Generic;
    using UnityEngine;


    public class HealthManager : MonoBehaviour {

    public int maxPlayerHealth;

    public static int playerHealth;

    Text text;

    private LevelManager levelManager;

    public bool isDead;

    private PlayerController player;

    private CheckPoint checkPoint;

    // Use this for initialization
    void Start () {
    text = GetComponent<Text>();

    playerHealth = maxPlayerHealth;

    levelManager = FindObjectOfType<LevelManager>();

    isDead = false;

    player = GetComponent<PlayerController>();







    }

    // Update is called once per frame
    void Update () {
    if (playerHealth <= 0 & !isDead)
    {
    player.transform.position = checkPoint.transform.position;
    player.enabled = false;
    playerHealth = 0;

    levelManager.respawnPlayer();

    isDead = true;
    }
    text.text = "" + playerHealth;

    }

    public static void HurtPlayer(int damageToGive)
    {
    playerHealth -= damageToGive;
    }
    public void FullHealth()
    {
    playerHealth = maxPlayerHealth;
    }

    }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
  3. Chris186

    Chris186

    Joined:
    Mar 26, 2014
    Posts:
    2
    Ok from what I see you have a variable called 'isDead' which tells your script when the player is dead right? Therefore, for this to have an impact on your player movement you need to include this in your player controller script.

    For example this script
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.W) && grounded)
    2.  
    3. {Jump();}
    4.  
    5. if (Input.GetKeyDown(KeyCode.W) && !doubleJump && !grounded)
    6.  
    7. {DoubleJump();
    8. doubleJump = true;}
    should be something along the lines of this
    Code (CSharp):
    1. if(!isDead)
    2. {
    3.     if (Input.GetKeyDown(KeyCode.W) && grounded)
    4.  
    5.     {Jump();}
    6.  
    7.     if (Input.GetKeyDown(KeyCode.W) && !doubleJump &&      
    8.     !grounded)
    9.  
    10.     {DoubleJump();
    11.     doubleJump = true;}
    12. }
     
unityunity