- Home /
C# InstaKill method for 2d game?
I am making a 2d platformer but I don't seem to get the hang of killing my player... The plan is to kill the player once the player collides with a specific game object with a Box Collider 2d (with "Is Trigger" enabled). I have placed the LevelManager script in a gameobject placed in the scene and added the Instakill method to the specific game object. Please help with the killing of my player! :) Here are parts (the relevant code) of the code for the killing of the player.
Here is my InstaKill method:
public class InstaKill : MonoBehaviour
{
public void OnTriggerEnter2D(Collider2D other)
{
var Player = other.GetComponent<Player>();
if (Player == null)
return;
LevelManager.Instance.KillPlayer();
}
}
Here is the relevant parts of my LevelManager:
public class LevelManager : MonoBehaviour{
public static LevelManager Instance { get; private set;}
public void Awake()
{
Instance = this;
}
public void KillPlayer()
{
StartCoroutine(KillPlayerCo());
}
private IEnumerator KillPlayerCo()
{
Player.Kill();
Camera.IsFollowing = false;
yield return new WaitForSeconds(2f);
Camera.IsFollowing = true;
}
}
And finally the relevant parts of my Player script:
public class Player : MonoBehaviour
{
private CharacterController2D _controller;
public bool IsDead { get; private set; }
public void Update()
{
if(!IsDead)
HandleInput();
var movemenFactor = _controller.State.IsGrounded ? SpeedAcceleratioOnGround : SpeedAccelerationInAir;
if (IsDead)
_controller.SetHorizontalForce(0);
else
_controller.SetHorizontalForce(Mathf.Lerp(_controller.Velocity.x, _normalizedHorizontalSpeed *
MaxSpeed, Time.deltaTime * movemenFactor));
}
public void Kill()
{
_controller.HandleCollisions = false;
GetComponent<Collider2D>().enabled = false;
IsDead = true;
_controller.SetForce(new Vector2(0, 10));
}
}
Answer by ThePeat · Apr 12, 2018 at 12:25 AM
Well if you simply want to remove/destroy the player then... Unity API Docs - Destroy
And it would look like this in your player script:
public void Kill()
{
_controller.HandleCollisions = false;
GetComponent<Collider2D>().enabled = false;
IsDead = true;
_controller.SetForce(new Vector2(0, 10));
// I am assuming the player script is attached to the root Gameobject of the player
Destroy(this.gameObject);
}
If you want to play an animation when he dies, you have to make an function call in your death-animation where you destroy/remove the player.
Answer by oddspacefalcon · Apr 12, 2018 at 09:02 AM
I see, however it seems that I failed to mention that when the player hits the game object that should kill the player, nothing happens (it acts like normal ground). Could that indicate that the OnTriggerEnter2D (in InstaKill) or the KillPlayer method (in Level Manager) is not triggered/called in the first place? Furthermore I have made it so that two seconds after the death of the player, the player will respawn at the latest checkpoint (that code is not included above), that is why I have in the Kill method put the
_controller.HandleCollisions = false;
GetComponent<Collider2D>().enabled = false;
and
_controller.SetForce(new Vector2(0, 10));
in order for the player to jump up and then fall down and dissapear before respawning. However this can't happen if not the Kill method gets called.