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 · Oct 26, 2016 at 11:58 PM · 2d game2d-gameplaysimulationdepth

2D depth simulation 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 BACKGROUND objects collider (TurnOnCollisionForeground)

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

TurnOffCollisionForeground

 using UnityEngine;
 using System.Collections;
 
 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

 using UnityEngine;
 using System.Collections;
 
 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

 using UnityEngine;
 using System.Collections;
 
 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

 using UnityEngine;
 using System.Collections;
 
 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:

 using UnityEngine;
 using System.Collections;
 
 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)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JincSoft · Oct 27, 2016 at 08:05 AM

In all of your scripts Update() function, you are always enabling the collider regardless of what your SendStatus script is doing. To be honest you don't need a 4 scripts to manage this, you could get away with just one.

  1. Separate all the objects into empty game objects (one for foreground, one for background)

  2. In the SendStatus script (or a new one since it doesn't send any status anymore), have two public GameObject variables (or a GameObject array if you are ok with that style) that you assign in the inspector for the two empty objects acting as parents.

  3. In the Start() function, loop through the two GameObjects and either add the colliders from the children to respective Lists (recommended) or add them to respective Collider2D arrays for caching and easier access.

  4. Write one function that will loop through a given List/Array that will invert the current value (example invert using your code, isOnForeground = !isOnForeground)

  5. Call that function only when you are intending to switch from the foreground and the background so that you don't even need an Update() function.

That should solve most of the issues you may have with placed prefabs but if you are trying to have enemies that dynamically spawn and move, you cannot use this method on them. For that you would need to add the enemy to the list of objects for it's respective depth as soon as it spawns (which is why I recommend you use a List). If you need more help or have questions feel free to ask.

C# List Info

https://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

Finding Components In Children

https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html

Comment
Add comment · Show 3 · Share
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 luigiwagner · Nov 02, 2016 at 06:43 PM 0
Share

Hey man, thank for the help already, but I´m pretty new to Unity (to game development in general, actually), so I´m having a little bit of trouble figuring out how to do everything you suggested.

As you said that the SendStatus script was unnecessary, I got rid of it and created a PlayerStatus script ins$$anonymous$$d.

Like you suggested, I also created two empty game objects in the scene to be filled with the foreground and background prefabs accordingly, along with two public game objects in the PlayerStatus, for the background and foreground obstacles.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerStatus : $$anonymous$$onoBehaviour {
 
     public GameObject foregroundObstacle;
     public GameObject backgroundObstacle;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
 

Now regarding step 3, how do I loop through the game objects and how would I go into adding the colliders to them? Also, what type and what would the Lists be consisted of (and if possible, a coding help regarding this) ?

I haven´t actually worked with arrays in Unity yet, so I also can´t quite grasp the alternative solution regarding the Collider2D array.

Because of that, I´m obliviously having trouble on how to implement the function on step 4 too.

Lastly, where would I call said function like you said on step 5?

And just an observation, if I didn´t make it clear before, my intent is to spawn the obstacles randomly at run time through prefabs.

Thanks in advance!

avatar image JincSoft luigiwagner · Nov 02, 2016 at 08:11 PM 0
Share

So Unity answers is dumb and won't let me upload a txt file so here is a script with comments in it. Lists are better than arrays because you can reorder them, sort them, make them bigger at runtime, the list goes on (very punny). Sometimes (as used in the script here) arrays are better for short tasks like in the Start() function.

This should give you something to work with for when you go for randomly spawned objects and whatnot. There are plenty of tutorials written regarding spawning things at runtime, performance things, etc so I won't go into that but this script can be easily modified for runtime generation things.

avatar image luigiwagner JincSoft · Nov 03, 2016 at 12:02 AM 0
Share

Thank you so much! I´m currently busy with a lot of college stuff, but when I get the time to work on this I´ll surely do. But for now, I really appreciate the help!

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

81 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

Related Questions

2D depth in a sidescroller 0 Answers

Simulating 2D depth in a sidescroller 0 Answers

Simulating depth on a 2D sidescroller 2 Answers

clamp position 1/4 of a circle 2D 1 Answer

How to fix knockback in 2D melee combat system 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