• 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 /
avatar image
0
Question by Stareyedraccoon · Aug 12, 2019 at 03:17 PM · 2dscene-switchingdoordoors

Starting point for leaving each scene?

hello! Im new to unity so please bear with me! Im trying my hand at making a game in 2D where you can enter houses and exit back to the main menu. I have 2 questions! 1- is there a way to set the Player to be facing a certain way when entering a house? 2- can I set different starting points for the main map depending on where you exit from?

thank you!

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
3
Best Answer

Answer by I_Am_Err00r · Aug 13, 2019 at 01:32 PM

Short answer is yes, you can absolutely do all that.


To give you more direction, it really depends on a lot of what you already have written, but depending on your proficiency with code you might be able to take my advice and utilize it, but if you want a plug and play solution that you can just copy from here and paste into your game it depends on how you instantiate the player when loading scenes mainly and how many scripts are involved with that process.


To give you general direction, you can save a bool in PlayerPrefs like this:

 PlayerPrefs.SetInt("FaceLeft"),   1 or 0);
 PlayerPrefs.GetInt("FaceLeft"),  1 or 0);

And the reason I mention that is you will need to create a bool that will notify your instantiation script what direciton you plan on having them face when being instantiated (the logic behind that is that 1 will be true, 0 will be false, and when you set an int to 1 or 0 like that, you get a handy bool that isn't supported by playerprefs without a little creativity) and I would put a public bool shouldFaceLeft on the door script to trigger the event:

 OnTriggerEnter2D(Collider col)
      {
         if(shouldFaceLeft == true)
         {
             PlayerPrefs.SetInt("FaceLeft", 1);            
         }
      }  

Then when you load the scene, on whatever script that instantiates your player into the scene, do a check like this:

 if(PlayerPrefs.GetInt("FaceLeft") == 1)
 {
      spriteRendererReference.flipX = true;
 }

And that will be able to flip the sprite as it's loaded.


As far as instantiating at a certain point on the main map, this might get you in the right direction, but I would create:

 public List<Transform> playerPositions = new List<Transform>();

of starting points on your main map, and then reference that list based on list[int] in the playerprefs again; so for example if you want to have list[0] = (0,0,0), then in your main scene, create an empty game object at that position, add publicly to the list as the first element, and in the door collider script where you set the bool, also set a public int spawnNumber (for this example, set spawnNumber to 0 in the inspector) and then do this:

 PlayerPrefs.SetInt("Spawn", spawnNumber);

And again in your scene loading script do something like this:

 Vector3 startingPosition = playerPositions[ PlayerPrefs.GetInt("Spawn")].position;

And then you can publicly populate the rest of your starting positions with empty game objects peppered throughout the main scene at the places you want player to start; when you want to reference a different position in the list, just make sure that door collider has the spawnNumber setup correctly. REMEMBER, LISTS AND ARRAYS BEGIN WITH 0, NOT 1.


NONE OF THIS IS TESTED, NONE OF THIS WAS WRITTEN IN AN IDE, THERE MAY BE SYNTAX AND COMPILER ERRORS, BUT THIS SHOULD BE A SOLUTION TO AT LEAST GET YOU STARTED

Comment
Add comment · Show 6 · 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 Stareyedraccoon · Aug 16, 2019 at 08:10 AM 0
Share

Thank you so much for your reply! to give you a little more context; I dont know how to code at all! Im simply trying to $$anonymous$$ch myself how to make a simple farmer game following youtube tutorials

I understand your reply for the most part, Im just not sure how to implement it (again; learning through youtube tutorials, trying my best!)

what i have written (in C##) so far is a PlayerController, which lets me move in all directions, a Door, which lets me enter the next scene by pressing E, and a SpawnPoint, which lets me choose the players spawn point in One Scene only )-: if you'd like the code for any of these feel free to let me know. So i suppose my next question is; Could you please expand on where exactly to write these codes? (ex; "On the door leading into the next scene, in the Void Start" etc etc) thanks so much im a dumbass

avatar image I_Am_Err00r Stareyedraccoon · Aug 16, 2019 at 01:45 PM 0
Share

If you provide the code you use for the door and the code you use to instantiate you player in the next scene, I could add all the variables and functions for you.


Just so you know, I use this forum to pass time at my current job while I transition out of my career in sales into game dev, so I don't have a compiler or Unity to test, so if the codes I return to you don't work, please let me know; I'm super bored at my job all day long and this helps pass the time for me while sharpening my code skills too, so feel free to reach out if you need further explanations or something is giving you an error.

avatar image Stareyedraccoon I_Am_Err00r · Aug 17, 2019 at 01:28 AM 0
Share

ty!!! ill use going in and out of the bar as an example- to enter the bar from the main map, and exit back out, i use the same code,

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bardoorexit : $$anonymous$$onoBehaviour
 { 
     public int LevelToLoad;
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.CompareTag("Player"))
         {
             if (Input.Get$$anonymous$$eyDown("e"))
             {
                 Application.LoadLevel(LevelToLoad);
             }
         }
     }
     private void OnTriggerStay2D(Collider2D collision)
     {
         if (collision.CompareTag("Player"))
         {
             if (Input.Get$$anonymous$$eyDown("e"))
             {
                 Application.LoadLevel(LevelToLoad);
             }
         }
     }
     private void OnTriggerExit2D(Collider2D collision)
     {
         if (collision.CompareTag("Player"))
         {
             if (Input.Get$$anonymous$$eyDown("e"))
             {
                 Application.LoadLevel(LevelToLoad);
             }
         }
     }
 }
 

and attach the code to an empty game object with a box collider, changing the 'level to load' to the scene number (0 being the main map, 1 being the bar) I made this code by following a youtube tutorial, so if its terrible be easy on me lol

I also have a 'start point' code for when the character enters the bar, which is what was giving me difficulty with the sprite facing the wrong way

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Startpoint_Bar : $$anonymous$$onoBehaviour
 {
     private PlayerController thePlayer;
     // Start is called before the first frame update
     void Start()
     {
         thePlayer = FindObjectOfType<PlayerController>();
         thePlayer.transform.position = transform.position;
     }
 }
 
 

and im not sure how to go about making multiple starting points for the main map, triggered by which scene they just left thank you for your time!

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

216 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make doors and roofs with Pro Builder? 0 Answers

What is wrong with my script for Opening a door on click 1 Answer

door that closes by itself 2 Answers

Megaman style door 0 Answers

Private and public variables 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges