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 /
  • Help Room /
avatar image
0
Question by bybocek · Mar 04, 2017 at 01:15 PM · playerkillallallocationkill 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
Add comment · Show 2
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 Pengocat · Mar 04, 2017 at 07:21 PM 0
Share

If you refer to public void OnTriggerEnter2D not being called that is probably because you did not flag the collider as trigger (checkbox on the collider component).

avatar image Commoble · Mar 04, 2017 at 07:59 PM 0
Share
     var player = other.GetComponent();
     if (player == null) return;
     Level$$anonymous$$anager.Instance.$$anonymous$$illPlayer();

This should be Player player = other.GetComponent<Player>(). Otherwise it'll return any ol' component on the other object... maybe? The documentation doesn't even define your version of that function, I'm surprised it's syntactically valid. It's generally good practice to read the documentation on a function before you use it.

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.

Follow this Question

Answers Answers and Comments

100 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 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

Fatal error! What happened? 0 Answers

Object Player Movement stops working after Restarting Unity 0 Answers

Mining from a chunk? - 3d Game Cavern Generation + Player integration 0 Answers

Player (Using Player Controller) slow falls when I repeatedly press the jump button while falling., 0 Answers

Updating Mesh Collider with Player Mesh 0 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