Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by luigiwagner · Mar 28, 2017 at 10:35 PM · 2d game2d-platformer2d-gameplaysimulationdepth

2D depth in a sidescroller

I am developing a 2D sidescroller. One of the basic mechanics of the game is that the player can choose between being on the foreground of the level or on the background of the level (deviating from the obstacles accordingly).

Since I´m developing it in pure 2D form, I´m having to "simulate" the depth of the level.

For that, I created four scripts:

One of them turns OFF the FOREGROUND objects collider (TurnOffCollisionForeground)

The other turns OFF the BACKGROUND objects collider (TurnOffCollisionBackground)

The other turns ON the FOREGROUND objects collider (TurnOnCollisionForeground)

And the other turns ON the BACKGROUND objects collider (TurnOnCollisionBackground)

TurnOffCollisionForeground

 public class TurnOffCollisionForeground : MonoBehaviour
  {
      private BoxCollider2D boxCollider;  
  
      // Use this for initialization
      void Start()
      {
          boxCollider = GetComponent<BoxCollider2D>();      
      }
  
      public void OnCollisionEnter2D()
      {
          boxCollider.enabled = false;
      }
  
      // Update is called once per frame
      void Update()
      {
          OnCollisionEnter2D();
      }
  }

TurnOffCollisionBackground

 public class TurnOffCollisionBackground : MonoBehaviour
  {
  
      private BoxCollider2D boxCollider;
  
      // Use this for initialization
      void Start()
      {
          boxCollider = GetComponent<BoxCollider2D>();      
      }
  
      public void OnCollisionEnter2D()
      {
          boxCollider.enabled = false;
      }
  
  
      // Update is called once per frame
      void Update()
      {
          OnCollisionEnter2D();
      }
  }

TurnOnCollisionForeground

  public class TurnOnCollisionForeground : MonoBehaviour {
  
      private BoxCollider2D boxCollider;
  
      // Use this for initialization
      void Start()
      {
          boxCollider = GetComponent<BoxCollider2D>();
      }
  
      public void OnCollisionEnter2D()
      {
          boxCollider.enabled = true;
      }
  
  
      // Update is called once per frame
      void Update()
      {
          OnCollisionEnter2D();
      }
  }

  

TurnOnCollisionBackground

  public class TurnOnCollisionBackground : MonoBehaviour {
  
      private BoxCollider2D boxCollider;
  
      // Use this for initialization
      void Start()
      {
          boxCollider = GetComponent<BoxCollider2D>();
      }
  
      public void OnCollisionEnter2D()
      {
          boxCollider.enabled = true;
      }
  
  
      // Update is called once per frame
      void Update()
      {
          OnCollisionEnter2D();
      }
  }

 

To check in what layer of depth the player is, I wrote the SendStatus script (which is attached to the player). In this script, inside the CurrentStatus() function (which is repeatedly called in Update() ) I check for player answer regarding the choice of layer - which he transits between by pressing the space bar.

The main thing here is that I want to use the SendStatus script to activate other scripts (TurnOffCollisionBackground, TurnOffCollisionForeground...) which are in OTHER game objects (prefab objects that contain said scripts).

Observation : on Start(), I initialize the player in the foreground layer (which is where he is supposed to start the game on)

To do that, I tried referencing those scripts in the SendStatus script and enabling/disabling them accordingly, as you can check below:

SendStatus

  public class SendStatus : MonoBehaviour {
  
      //This script in on the player
  
      public bool isOnForeground;
      public bool isOnBackground;
  
      private TurnOffCollisionBackground offBack;
      private TurnOnCollisionBackground onBack;
      private TurnOffCollisionForeground offFore;
      private TurnOnCollisionForeground onFore;
  
      // Use this for initialization
      void Start ()
      {
          isOnForeground = true;
          isOnBackground = false;
  
          offBack = GetComponent<TurnOffCollisionBackground>();
          onBack = GetComponent<TurnOnCollisionBackground>();
          offFore = GetComponent<TurnOffCollisionForeground>();
          onFore = GetComponent<TurnOnCollisionForeground>();
  
          offBack.enabled = true;
          onBack.enabled = false;
          offFore.enabled = false;
          onFore.enabled = true;
  
      }
  
      public void CurrentStatus()
      {
          if (Input.GetKeyDown(KeyCode.Space))
          {
              if (isOnForeground == true && isOnBackground == false) 
              {
                  isOnForeground = false;
                  isOnBackground = true;
  
                  offBack.enabled = false;
                  onBack.enabled = true;
                  offFore.enabled = true;
                  onFore.enabled = false;
  
              }
              if (isOnForeground == false && isOnBackground == true) 
              {
                  isOnForeground = true;
                  isOnBackground = false;
  
                  offBack.enabled = true;
                  onBack.enabled = false;
                  offFore.enabled = false;
                  onFore.enabled = true;
              }
          }
      }
      
      // Update is called once per frame
      void Update ()
      {
          CurrentStatus();
      }
  }

However, this solution is not working properly. When I run the game and I check the scripts in the obstacles, they don´t alternate accordingly (activating or deactivating).

------------------------------------------------------------------------------------------------------------------------------------- To sum up - my main problem is finding a way to activate and deactivate scripts on other game objects (the obstacles) from another script (SendStatus) in a different game object (the player)

I´ve been told that I might have to arrange all the obstacles in specific lists or arrays for this to work, but since I don´t have that much experience with working with arrays or lists on Unity, I´d really appreciate a guidance to do this, if it is, indeed, the proper solution.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toddisarockstar · Mar 29, 2017 at 03:28 AM 1
Share

lists and arrays are very important to learn. they make things so much easier and there are many things you simply cant do with out them so i highly suggest taking the time to learn.

anyways for now to fix your situation you could always give all the scripts a lookup back to the SendStatus script and check a common variable there as a trigger.

avatar image luigiwagner toddisarockstar · Mar 30, 2017 at 12:06 AM 0
Share

I´ll definitely give lists and arrays a study!

As for your suggestion, it sounds interesting. How would I make the scripts "look back" to the SendStatus one?

avatar image hexagonius · Mar 29, 2017 at 07:47 AM 1
Share

you can have it way easier. create two layers under Edit->Physics->Tags and Layers. uncheck their collision in the collision matrix. put the background colliders on one, foreground colliders on another layer. now you just need to toggle the players layer, nothing else (ok the renderer layer, but works the same way)

avatar image luigiwagner hexagonius · Mar 30, 2017 at 12:14 AM 0
Share

I already associated the background obstacles to a background layer and the foreground ones to a foreground layer. But come to think of it, I also associated the player to a different layer on itself (a player layer). Should I have done that? Can I change the player´s layer in runtime?

Also, what is the "collision matrix" and how could I uncheck the layers collision in it?

Lastly, what did you mean with "toggling the player´s layer" and how would I proceed on doing that?

avatar image luigiwagner hexagonius · Mar 31, 2017 at 02:24 AM 0
Share

Ok, I think I got what the collision matrix is. But I´ve only seen a way to manipulate it through the Inspector (Edit --> Project Settings --> Physics2D).

Is there a way I can manipulate it in runtime?

I´ve found the function public static void IgnoreLayerCollision(int layer1, int layer2, bool ignore) to make the player layer ignore the obstacles layers accordingly, but it still doesn´t seem to work.

Any tips to use this function? Or should I be using another one to manipulate the Collision $$anonymous$$atrix in runtime?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

103 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D depth simulation in a sidescroller 1 Answer

Unity 2D 5.2.2 How can I aim with keys towards an object? 2 Answers

2d platformer physics 0 Answers

2D Sprite rotation 180 degrees 0 Answers

How to make the next level load based on 2 characters touching a collider? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges