- Home /
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!
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
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
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.
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!