Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Checkpoint?

Discussion in 'Scripting' started by Shadowlash1221, Jan 20, 2016.

  1. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    I know i need to make a empty gameobject and add the checkpoint script to it, but how would i make a check point script? It's a 2d game by the way. Any help will help thanks!
     
  2. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    Here is some code that I was going to put on the asset store. This is a waypoint system.

    Usage:

    Way Points

    Way Points are used to create paths that an object must follow.

    Way Points can be used to create many game features such as a Tower defense path that enemies must take. It could also be used for a space shooter where enemies follow a path pattern.

    Creating the Path

    To create a Way Point system, you should place the `WayPoint System` prefab within the scene.
    You should then duplicate that `WayPoint` node to create your way point system.
    As you create your system you will see that it will draw lines to show you the path.

    Auto Following

    Game Objects can auto follow the path using the `Simple->Path->Auto Follow` script.
    Once added, you will need to assign it a Path which is attached to the `WayPoint System` prefab root element.
    Choose the settings that you would like and you're done.

    Events

    Events should be registered in `OnEnable` and should be removed in `OnDisable`.

    Code (csharp):
    1. OnPathStart(GameObject go)
    Called when a path is started.

    Code (csharp):
    1. OnPathFinished(GameObject go)
    Called when an object has finished its path.
     

    Attached Files:

  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    @The Little Guy "checkpoint" i.e. go past this point and when you die you respawn here... at least that's what I would understand as a checkpoint in gaming parlance.

    @Shadowlash1221 do you have a respawn mechanic yet?
     
  4. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    My waypoint system will allow you to do that. I didn't program that in though... So that part will have to be written
     
  5. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    no, this is my first time :/
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, what do you have?
     
  7. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    I used this but it didn't work :/
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelManager : MonoBehaviour {
    5.  
    6.     public GameObject currentCheckpoint;
    7.  
    8.     private SimplePlatformController player;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         player = FindObjectOfType<SimplePlatformController>();
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.    
    18.  
    19.     }
    20.     public void RespawnPlayer()
    21.     {
    22.         Debug.Log ("Player Respawn");
    23.         player.transform.position = currentCheckpoint.transform.position;  
    24.     }
    25. }
     
  8. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    But besides that iv'e got nothing :/
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, good start.

    How did you try to call RespawnPlayer()?, assuming it's something like mario etc. you'll need some sort of Triggers/Colliders setup in your scene to "kill" the player by calling
    Code (csharp):
    1.  
    2. FindObjectOfType<LevelManager>().RespawnPlayer();
    3.  
    in their OnTriggerEnter/OnCollisionEnter functions.
     
  10. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    And i put this on the enemy
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KillPlayer : MonoBehaviour {
    5.  
    6.     public LevelManager levelManager;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         levelManager = FindObjectOfType<LevelManager>();
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.        
    16.     }
    17.  
    18.     void OnTriggerEnter2D(Collider2D other)
    19.     {
    20.         if(other.name == "Player")
    21.         {
    22.             levelManager.RespawnPlayer();
    23.         }
    24.     }
    25. }
    26.  
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, assuming there is some code to make the player/enemy move around and actually collide... you're saying the collision code isn't called? have you tried putting Debug.Log(...) lines in at the very start of the OnTriggerEnter2D and RespawnPlayer to see if they are trying to be called?

    in 3d physics something involved in the collision needs to have a rigidbody attached to it, assuming thats true in the 2d engine as well does your player/enemies have rigidbody2d components attached?
     
  12. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    Debug.log won't work
     
  13. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Is the collider on your Enemy gameobject set to be "IsTrigger" ?
     
  14. Shadowlash1221

    Shadowlash1221

    Joined:
    Dec 30, 2015
    Posts:
    115
    Yes