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();
}
}
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).
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.
Your answer
Follow this Question
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