Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oddspacefalcon · Apr 11, 2018 at 09:36 PM · 2d gameplayercharacter controllerkillkill player

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));
   }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me with my player kill script? 0 Answers

Kill Player Method Doesn't Work ? 0 Answers

Raising hazard killing player 0 Answers

game goes slow and characters fall of through the terrain 1 Answer

How to get smooth 4 directional movement in top down without diagonal movement? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges