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. Dismiss Notice

How to make that 2 characters are touching a sprite to pass to the next level

Discussion in '2D' started by erymael17, Mar 5, 2021.

  1. erymael17

    erymael17

    Joined:
    Feb 8, 2021
    Posts:
    6
    Hi I'm trying to create a game that the user moves two characters at the same time. So in order to pass the stage both characters must be at the door. How can I make that on Unity??

    This is the coding I used if one of the characters touches the door but I'll like to make it for both characters

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

    public class endtrigger : MonoBehaviour
    {
    [SerializeField] private string newlevel;

    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.CompareTag("Player"))
    {
    SceneManager.LoadScene(newlevel);
    }

    }
    }
     

    Attached Files:

  2. AdamEarlston

    AdamEarlston

    Joined:
    Jun 7, 2019
    Posts:
    71
    Idk how to code at all, so i'm probably totally wrong, but wouldn't it be something like
    Code (CSharp):
    1. if (other.CompareTag("Player")) && (other.CompareTag("Player2")
    Basically add 2 tags, guessing the 2nd character has to have a different tag. But again i'm a total noob so i can only guess >.< Hopefully it was helpful.
     
  3. erymael17

    erymael17

    Joined:
    Feb 8, 2021
    Posts:
    6
    Adam thanks for the help but it didn't work. I think that I'll need to make two scripts for each door but with some type of coding that it won't work until the other character touches it's door.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class endtrigger : MonoBehaviour
    7. {
    8.     [SerializeField] private string newlevel;
    9.  
    10.     void OnTriggerEnter2D(Collider2D other)
    11.     {
    12.         if (other.CompareTag("Player"))
    13.         {
    14.             SceneManager.LoadScene(newlevel);
    15.         }
    16.    
    17.     }
    18. }