Problem with player dead and respawn
Hi guys! I'm making a 2d game for my exam's project but i have a problem. When the player touches the obstacle , he goes through him and not die , and consequently the checkpoint don't start. Anyone can help me? I've create 3 script for manage that. Here the code CHECKPOINT:
public class Checkpoint : MonoBehaviour {
public LevelManager levelManager;
// Use this for initialization
void Start () {
levelManager = FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update () {
}
void onTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
levelManager.currentCheckpoint = gameObject;
Debug.Log("Activated Checkpoint" + transform.position);
}
}
Here the code KILLPLAYER:
public class KillPlayer : MonoBehaviour {
public LevelManager levelManager;
// Use this for initialization
void Start () {
levelManager = FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update () {
}
void onTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
levelManager.RespawnPlayer();
}
}
}
And here the code LEVELMANAGER: public GameObject currentCheckpoint;
private PlayerController player;
public GameObject deathParticle;
public GameObject respawnParticle;
// Use this for initialization
void Start () {
player = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void Update () {
}
public void RespawnPlayer()
{
Instantiate(deathParticle, player.transform.position, player.transform.rotation);
Debug.Log("Player Respawn");
player.transform.position = currentCheckpoint.transform.position;
Instantiate(respawnParticle, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
}
}
Also, i post pictures with object attributes. Thanks to anyone will help me! :)
Answer by Tyche10 · May 11, 2016 at 09:13 AM
I think the problem is that you do not have a rigidbody attached to one of the two colliding objects. Without a rigidbody in at least one, the trigger isn't activated. Does your player have a rigidbody attached? (The unity documentation about OnTriggerEnter2D doesn't say this explicitly, but the OnTriggerEnter documentation does, so I think this is the problem: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html) Edit: I just noticed that you spelled OnTriggerEnter2D wrong in both scripts. It should start with a capital O.
Answer by DomenicoToddy · May 11, 2016 at 09:16 AM
Yes, my player have attached the Rigidbody 2D
I updated my answer. The problem is that you spelled the OnTriggerEnter2D function without a capital letter in the beginning, so unity doesn't recognize it.
@Tyche10 omg! ahahah what a fail! it works, thank you so much! :)
No problem ;) If you are satisfied by the answer you can accept it and close the question.
Your answer
Follow this Question
Related Questions
2D RigidBody slightly penetrates objects causing a bounce effect. 3 Answers
2d Plataform Player Walking on the Wall c# 0 Answers
Object sticks to the side of the collider 1 Answer
Attaching a gameObject above a rolling ball 1 Answer
How can I fix a null pointer exception? (2d platformer tutorial) 1 Answer