0
\$\begingroup\$

So what I have is a 2 platform game where I want my player to die when it hits a trap and go back to the last checkpoint it when past

Currently I use this script:

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

public class Respawn : MonoBehaviour
{
    public Transform PlayerPrefab;
    public Transform SpawnPoint;

    void OnCollisionEnter2D(Collision col)
     {
         if (col.gameObject.tag == "Trap")
         {
             respawn();
         }
    }
 

    void respawn()
    {
        Instantiate(PlayerPrefab, SpawnPoint.position, SpawnPoint.rotation);
    }
}

but When i hit the trap I go through it The trap does have a 2 collider on it with ontrigger Selected

How could I implement a checkpoint system and then death

\$\endgroup\$
13
  • \$\begingroup\$ Add a Debug.Log("Hit trap"); before the respawn in the trigger. So we know for sure if your problem lies in the respawn (you do not destroy the current player) or in reaching to the part of respawn. And where is this respawn script attached? On the player or the trap? And you have the wrong method signatur void OnCollisionEnter2D(Collision2D col). You are missing 2D on Collision \$\endgroup\$
    – Zibelas
    Feb 3, 2021 at 10:26
  • \$\begingroup\$ @Zibelas Hit trap works but I don't respawn What must I fix \$\endgroup\$
    – elliot727
    Feb 3, 2021 at 10:31
  • \$\begingroup\$ PlayerPrefab should be of type GameObject and not Transform \$\endgroup\$
    – Zibelas
    Feb 3, 2021 at 10:33
  • \$\begingroup\$ @Zibelas now I get this error : InvalidCastException: Specified cast is not valid. (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr) UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:281) Respawn.respawn () (at Assets/Respawn.cs:22) Respawn.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Respawn.cs:15) \$\endgroup\$
    – elliot727
    Feb 3, 2021 at 10:43
  • \$\begingroup\$ Well, do you have a PlayerPrefab? It is the a bluish looking cube in your project folder \$\endgroup\$
    – Zibelas
    Feb 3, 2021 at 11:04

1 Answer 1

1
\$\begingroup\$

The Collision action matrix section of this page is always a great reference to look at. It shows what collision detection/trigger messages are sent when two objects collide.

I would ensure that the player has a RigidBody2D (shouldn't matter if it's kinematic or not) and a Collider2D (with Trigger selected), and that the trap has a Collider2D (with trigger selected as well).

And the callback you should use for trigger messages is OnTriggerEnter2D(Collider2D other), not OnCollisionEnter2D(Collision col).

\$\endgroup\$
5
  • \$\begingroup\$ The last paragraph is what's definitely wrong here. The rest are things which could be wrong. \$\endgroup\$
    – Philipp
    Feb 3, 2021 at 10:12
  • \$\begingroup\$ Another thing that is definitely wrong is that there is no GameObject used in the Instantiate but a Transform. \$\endgroup\$
    – Zibelas
    Feb 3, 2021 at 10:39
  • \$\begingroup\$ @Zibelas that's probably not an issue. You're allowed to Instantiate a Game Object with a reference to any of its Components, including a Transform. It still spawns the whole object, not just one free-floating Component. \$\endgroup\$
    – DMGregory
    Feb 3, 2021 at 12:15
  • \$\begingroup\$ @DMGregory thanks, there is always something new to learn. \$\endgroup\$
    – Zibelas
    Feb 3, 2021 at 12:32
  • \$\begingroup\$ Make sure to test your theories first, to avoid misleading new users. There's a lot of misinformation about Unity or GameDev in general that spreads from folks sharing rules they've heard but never tested to see if they're valid. \$\endgroup\$
    – DMGregory
    Feb 3, 2021 at 12:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .