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
public void Respawn()
{
this.transform.position = respawnPoint.position;
this.GetComponent<playerHealth>().currentHealth = this.GetComponent<playerHealth>().maxHealth;
spawn = true;
if (this.tag.Equals("playerA"))
{
anim.SetBool("chickenDead", false);
}
else
{
anim.SetBool("horseDead", false);
}
}
Spawning in the Update
void Update()
{
if(!spawn)
{
CheckMovementDirection();
UpdateAnimations();
CheckIfCanJump();
CheckDash();
CheckInput();
Fall();
}
if(spawn)
{
canMove = false;
spawnTime -= Time.deltaTime;
}
if(spawnTime <= 0)
{
canMove = true;
spawn = false;
spawnTime = 5.0f;
}
last_pos = current_pos;
current_pos = transform.position;
}
Checking the Movement Input and Applying Movement
private void CheckInput()
{
movementInputDirection = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump"))
{
Jump();
}
if (Input.GetButtonDown("Dash"))
{
if(Time.time >= (lastDash + dashCooldown))
{
dashDirection = "neutral";
AttemptToDash();
}
}
if (Input.GetKey(KeyCode.S) && Input.GetButtonDown("Dash"))
{
if (Time.time >= (lastDash + dashCooldown))
{
dashDirection = "down";
AttemptToDash();
}
}
if (Input.GetButtonDown("Horizontal") && Input.GetKey(KeyCode.S) && Input.GetButtonDown("Dash"))
{
if (Time.time >= (lastDash + dashCooldown))
{
dashDirection = "dia-down";
AttemptToDash();
}
}
if (Input.GetKey(KeyCode.S) && Input.GetButtonDown("Dash"))
{
if (Time.time >= (lastDash + dashCooldown))
{
dashDirection = "up";
AttemptToDash();
}
}
if (Input.GetButtonDown("Horizontal") && Input.GetKey(KeyCode.W) && Input.GetButtonDown("Dash"))
{
if (Time.time >= (lastDash + dashCooldown))
{
dashDirection = "dia-up";
AttemptToDash();
}
}
}
private void ApplyMovement()
{
if (canMove)
{
rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
}
}