Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How do you create a victory zone?

Discussion in 'Getting Started' started by Asough, Mar 2, 2024.

  1. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    How do you create a victory zone that marks the map as completed, as soon as the player reaches it?

    Screenshot.png

    So far, I have put up a sign, which is supposed to mark the map as completed, as soon as the player reaches it.

    I have also created an empty gameobject called victory and added a Box Collider 2D to it.

    Screenshot.png

    I then put it in the same place as the sign.

    I have also created a new tag called Victory and added it to the game object as well.

    So I guess it's time for me to write some code, or am I missing anything?

    This was I wrote so far:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Victory : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     public void OnTriggerEnter (Collider coll)
    9.     {
    10.         if (coll.CompareTag("Player"))
    11.         {
    12.  
    13.         }
    14.     }
    15.  
    16.     // Update is called once per frame
    17. }
    What else should I put in my script?
     
    Last edited: May 20, 2024
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,135
    A simple approach is to just load a new victory scene / screen and be done.

    If you want to stay in the current game for some kind of victory sequence then roughly you would need to:

    - inhibit player control
    - start the victory animation
    - when that animation is done, move onto whatever is next
     
  3. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    Yeah, I'm not really there yet.

    I just want the game to register that the map has been completed, as soon as the player reaches the sign, like I said.

    I also do not want a victory scene, at least not for now.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,508
    It might be working as you expect. Use a
    Debug.Log("Victory!");
    statement inside your block to see if it got there. If so, it's working and you can add whatever other behavior you want there.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,135
    Be sure you are using the correct physics system: this is the 2D forum but I see you using the 3D version of OnTriggerEnter(), so you probably want to start with some basic tutorials.

    The conditions necessary are laid out in great detail in the documentation.

    What does "register" mean?

    Then start with some tutorials. Asking detached random questions in a forum is going to make it infinitely harder to learn what you need.

    Imphenzia: How Did I Learn To Make Games:

     
  6. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    Um, where should I put it, exactly?

    Where can I see if I'm using the 2D version or the 3D version?

    Also, how do I change it back to the 2D version again?

    What I mean is that I want the game to detect that the map has been completed by the player, I thought that was obvious?
     
    Last edited: Mar 4, 2024
  7. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    166
    There are simply different function definitions for 2D and 3D collision/trigger interactions:

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D collision)
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,603
  9. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    Do you mean like this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Victory : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     private void OnTriggerEnter2D(Collider2D collision)
    9.     {
    10.         if (collision.CompareTag("Player"))
    11.         {
    12.  
    13.         }
    14.     }
    15.  
    16.     // Update is called once per frame
    17.  
    18. }
    Is this correct?

    To be fair, I have already been watching a lot of tutorials, but I couldn't find anything regarding this issue.

    But I will watch some more tutorials.
     
    Last edited: Mar 4, 2024
  10. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    166
    Yes, that looks fine to me. You should also doublecheck that you have set the "Is Trigger" checkbox on the BoxCollider2D of the Victory object. As mentioned elsewhere in this thread, you can put a Debug.Log() print inside your if statement to get some indication that the code reached the expected point.
     
    Asough likes this.
  11. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    Yeah, sorry I forgot to mention that I have already checked the is trigger checkbox on the victory object.

    Anyway, I just put..

    Code (CSharp):
    1. Debug.Log("Victory!");
    .. inside of the if statement, and there's now a message in the bottom left corner of Unity that says victory when I reach the sign, thanks!
     
    Last edited: Mar 6, 2024
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,603
    Tutorials are not problem solving so I don't follow what it means to try to find a tutorial on how to solve using the correct callback. Every single tutorial involving 2D physics callbacks would show the correct callback. :)

    Regardless, you seem to have solve your problem by using the correct callback signature which is great.