Search Unity

Question Player keeps moving after death/avoiding collisions with other players and projectiles after death

Discussion in 'Scripting' started by fmoodey, Apr 5, 2021.

  1. fmoodey

    fmoodey

    Joined:
    Jan 30, 2021
    Posts:
    3
    Part 1
    New to Unity and C# looking for a little help. So, I'm making a 2D fighting game where a character has multiple lives per round. An issue I'm having is that after the player dies from running through a spike trap, it continues to run in that direction continuously after respawning (and stops doing this after the user regains control when the 5s time delay has passed).

    I thought my solution would be to set the players velocity to 0 through Vector3.zero, but this hasn't worked thus far.

    Anybody have any ideas on how to stop the player from getting stuck moving right or left after respawning?

    Part 2
    Since it is a fighting game, I don't the player to respawn instantly and want a few moments where the players "dead state" is lying on the stage. The opponent should be able to run over the the corpse of the player, but not be able to collider or interact with it. Is there a way to disable collisions without having the player fall though the stage?

    Thanks in advance to anyone that helps out, much appreciated.

    Relevant Code

    Respawning

    Code (CSharp):
    1.     public void Respawn()
    2.     {
    3.         this.transform.position = respawnPoint.position;
    4.         this.GetComponent<playerHealth>().currentHealth = this.GetComponent<playerHealth>().maxHealth;
    5.         spawn = true;
    6.  
    7.         if (this.tag.Equals("playerA"))
    8.         {
    9.             anim.SetBool("chickenDead", false);
    10.         }
    11.         else
    12.         {
    13.             anim.SetBool("horseDead", false);
    14.         }
    15.     }
    Spawning in the Update
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if(!spawn)
    4.         {
    5.             CheckMovementDirection();
    6.             UpdateAnimations();
    7.             CheckIfCanJump();
    8.             CheckDash();
    9.             CheckInput();
    10.             Fall();
    11.         }
    12.  
    13.  
    14.         if(spawn)
    15.         {
    16.             canMove = false;
    17.             spawnTime -= Time.deltaTime;
    18.         }
    19.  
    20.         if(spawnTime <= 0)
    21.         {
    22.             canMove = true;
    23.             spawn = false;
    24.             spawnTime = 5.0f;
    25.         }
    26.      
    27.  
    28.     last_pos = current_pos;
    29.     current_pos = transform.position;
    30.     }
    Checking the Movement Input and Applying Movement
    Code (CSharp):
    1.     private void CheckInput()
    2.     {
    3.         movementInputDirection = Input.GetAxisRaw("Horizontal");
    4.  
    5.         if (Input.GetButtonDown("Jump"))
    6.         {
    7.             Jump();
    8.         }
    9.  
    10.         if (Input.GetButtonDown("Dash"))
    11.         {
    12.             if(Time.time >= (lastDash + dashCooldown))
    13.             {
    14.                 dashDirection = "neutral";
    15.                 AttemptToDash();
    16.             }
    17.         }
    18.  
    19.         if (Input.GetKey(KeyCode.S) && Input.GetButtonDown("Dash"))
    20.         {
    21.             if (Time.time >= (lastDash + dashCooldown))
    22.             {
    23.                 dashDirection = "down";
    24.                 AttemptToDash();
    25.             }
    26.         }
    27.  
    28.         if (Input.GetButtonDown("Horizontal") && Input.GetKey(KeyCode.S) && Input.GetButtonDown("Dash"))
    29.         {
    30.             if (Time.time >= (lastDash + dashCooldown))
    31.             {
    32.                 dashDirection = "dia-down";
    33.                 AttemptToDash();
    34.             }
    35.         }
    36.  
    37.         if (Input.GetKey(KeyCode.S) && Input.GetButtonDown("Dash"))
    38.         {
    39.             if (Time.time >= (lastDash + dashCooldown))
    40.             {
    41.                 dashDirection = "up";
    42.                 AttemptToDash();
    43.             }
    44.         }
    45.  
    46.         if (Input.GetButtonDown("Horizontal") && Input.GetKey(KeyCode.W) && Input.GetButtonDown("Dash"))
    47.         {
    48.             if (Time.time >= (lastDash + dashCooldown))
    49.             {
    50.                 dashDirection = "dia-up";
    51.                 AttemptToDash();
    52.             }
    53.         }
    54.  
    55.  
    56.     }
    57.  
    58.     private void ApplyMovement()
    59.     {
    60.         if (canMove)
    61.         {
    62.  
    63.             rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
    64.         }
    65.        
    66.     }
     
  2. jasonasaad2

    jasonasaad2

    Joined:
    Nov 30, 2020
    Posts:
    51
    you can create a new vector2, and use 0,0,0. Idk if it will work, but try it
     
    fmoodey likes this.
  3. fmoodey

    fmoodey

    Joined:
    Jan 30, 2021
    Posts:
    3
    That worked. Very weird, not sure why Vector3.zero didn't have the same effect. Thanks!
     
    jasonasaad2 likes this.