• 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
Question by bybocek · Mar 05, 2017 at 03:34 PM · playerkillkill player

Kill Player Method Doesn't Work ?

Can you help me with this? Punji sticks "I added Box Collider." "I added Instakill.cs." But he does not kill the player!

LevelManager.cs

using UnityEngine; using System; using System.Linq;

public class LevelManager : MonoBehaviour { public static LevelManager Instance {get; private set;}

 public Player Player {get; private set;}

 public CameraController Camera {get; private set;}

 private List<Checkpoint> _checkpoints;
 private int _currentCheckpointIndex;

 public Checkpoint DebugSpawn;

 public void Awake()
 {
     Instance = this;
 }

 public void Start ()
 {
     // sorts check points by X axis 
     _checkpoints = FindObjectsOfType<Checkpoint>().OrderBy(t => t.transform.position.x).ToList();

     // if check point  are more then 0 set checkpoint count to 0 is no checkpoints set checkpoints count to -1
     _currentCheckpointIndex = _checkpoints.Count > 0 ? 0 : -1;

     Player = FindObjectOfType<Player>();
     Camera = FindObjectOfType<CameraController>();

     //debug if will not appear outside of unity editor         
     #if UNITY_EDITOR      
     if (DebugSpawn != null)
         DebugSpawn.SpawnPlayer(Player);
     else if (_currentCheckpointIndex != -1)
         _checkpoints[_currentCheckpointIndex].SpawnPlayer(Player);
     #else
     //standard "production" if
     if (_currentCheckpointIndex != -1)
     _checkpoints[_currentCheckpointIndex].SpawnPlayer(Player);
     #endif
 }
 public void Update()
 {
     //checks if at last checkpoint
     var isAtLastCheckpoint = _currentCheckpointIndex + 1 >= _checkpoints.Count;
     if (isAtLastCheckpoint)
         return;

     //checks distance from next check point
     var distanceToNextCheckpoint = _checkpoints[_currentCheckpointIndex +1].transform.position.x - Player.transform.position.x;
     if (distanceToNextCheckpoint >= 0)
         return;

     //registers player leaving last checkpoint 
     _checkpoints[_currentCheckpointIndex].PlayerLeftCheckpoint();
     //updates checkpoint index
     _currentCheckpointIndex++;
     //Player hit new checkpoint
     _checkpoints[_currentCheckpointIndex].PlayerHitCheckpoint();

     //TODO: adjustment for player hitting a check point
 }

 public void KillPlayer()
 {
     StartCoroutine(KillPlayerCo());
 }

 private IEnumerator KillPlayerCo()
 {
     Player.Kill();
     Camera.IsFollowing = false;
     //gives control to unity for float #
     yield return new WaitForSeconds(2f);

     Camera.IsFollowing = true;

     if (_currentCheckpointIndex != -1)
         _checkpoints[_currentCheckpointIndex].SpawnPlayer(Player);

     //TODO: death adjustments
 }

}


CheckPoint.cs

using System.Collections; using UnityEngine;

public class Checkpoint : MonoBehaviour {

 public void PlayerHitCheckpoint()
 {
 }

 private IEnumerator PlayerHitCheckpointCo(int bonus)
 {
     yield break;
 }

 public void PlayerLeftCheckpoint()
 {
 }

 public void SpawnPlayer(Player player)
 {
     player.RespawnAt(transform);
 }

 public void AssignObjectToCheckpoint()
 {
 }

}


Player.cs


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

public class Player : MonoBehaviour { private bool _isFacingRight; private CharacterController2D _controller; private float _normalizedHorizontalSpeed;

 public float MaxSpeed = 12;
 public float SpeedAccelerationOnGround = 10f;
 public float SpeedAccelerationInAir = 5f;

 public bool IsDead { get; private set; }

 public void Awake()

 {
     _controller = GetComponent<CharacterController2D> ();
     _isFacingRight = transform.localScale.x > 0;

 }

 public void Update ()
 {
     if (!IsDead)
     HandleInput ();

     var movementFactor = _controller.State.IsGrounded ? SpeedAccelerationOnGround : SpeedAccelerationInAir;
     _controller.SetHorizontalForce (Mathf.Lerp (_controller.Velocity.x, _normalizedHorizontalSpeed * MaxSpeed, Time.deltaTime * movementFactor));

 }

 public void Kill()
 {
     _controller.HandleCollisions = false;
     this.gameObject.GetComponent<Collider2D>().enabled = false;
     IsDead = true;
 }

 public void RespawnAt(Transform spawnPoint)
 {
     if (!_isFacingRight)
         Flip ();
     IsDead = false;
     this.gameObject.GetComponent<Collider2D>().enabled = true;
     _controller.HandleCollisions = true;

     transform.position = spawnPoint.position;
 }

 private void HandleInput()
 {
     if (Input.GetKey (KeyCode.D)) 
     {
         _normalizedHorizontalSpeed = 1;
         if (!_isFacingRight)
             Flip ();
     } 
     else if (Input.GetKey (KeyCode.A)) 
     {
         _normalizedHorizontalSpeed = -1;
         if (_isFacingRight)
             Flip ();
     } 
     else 
     {
         _normalizedHorizontalSpeed = 0;
     }
     if (_controller.CanJump && Input.GetKeyDown (KeyCode.Space)) 
     {
         _controller.Jump ();
     }
 }

 private void Flip()
 {
     transform.localScale = new Vector3(-transform.localScale.x,transform.localScale.y,transform.localScale.z);
     _isFacingRight = transform.localScale.x > 0;
 }

}


InstaKill.cs


using UnityEngine;

public class InstaKill : MonoBehaviour { public void OnTriggerEnter2D(Collider2D other) { var player = other.GetComponent(); if (player == null) return;

     LevelManager.Instance.KillPlayer();
 }

}

Comment

People who like this

0 Show 1
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 OusedGames · Mar 06, 2017 at 12:51 AM 0
Share

Hello, I think the problem is on the OnTriggerEnter2D of "InstaKill.cs"

  • void OnTriggerEnter2D(Collider2D col) { if (col.GetComponent<Player>() != null) { LevelManager.instance.KillPlayer(); } }


  • Try cheking the component like I did above, Hope it works

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

C# InstaKill method for 2d game? 2 Answers

Raising hazard killing player 0 Answers

How to make an AI like slender ? 1 Answer

Killer + assistance id for shooter game 1 Answer


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