- Home /
2D Platformer multiple spawnpoints
I'm making a 2D platformer game right now, and i want to move between areas but i want some areas to have multiple spawnpoints. The way it works, A player goes through a portal and it loads a new scene, and in that new scene there are another 2 portals, one that loads the scene the player came from(I will call it portal 1) and another portal that loads the next scene(and this portal i will call portal 2). Now i want the player to spawn in the portal 1 because that's the one that will lead him back to the scene that he just came from. Now i could just set the spawnpoint in portal 1 but the problem is, that portal 2 loads the next scene where in that next scene, you can come back to portal 2, But how can i make the player spawn in portal 2 if there is a spawnpoint in portal 1? Now because i'm very bad at explaining i will try to draw it,I will appreciate every help, Thank you! I'm getting an error when trying to upload the image so i uploaded it to google drive: https://drive.google.com/file/d/1ydIXKw8LfFK_DvdgXRyFChk_zHbC7A44/view?usp=sharing (The circle in the middle is another scene btw)
Answer by Optrix · Jul 01, 2019 at 06:04 AM
You can use a static variable, or alternatively you can have a GameObject that uses https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
DontDestroyOnLoad allows a GameObject to persist between scenes.
You can have a GameObject containing the full state of your game - number of lives, player name, Facebook PI details, and the name of the last portal they passed through - and any required network connections etc. that survives between scenes. When you load the new scene, you check which portal they used and choose which spawn point to pick.
Yeah i know what is DontDestroyOnLoad but i', not sure how can i know which portal i went trough but i think i have an idea. Every time that i use a Portal, it will send the name of the portal to the gameobject but i'm thinking how. Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
public class Portal : $$anonymous$$onoBehaviour
{
public bool CanTeleport;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
if(CanTeleport == true)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow))
{
Scene$$anonymous$$anager.LoadScene("NextWorld");
}
}
}
private void OnTriggerStay2D(Collider2D collision)
{
if(collision.gameObject.tag == "Player")
{
CanTeleport = true;
}
else
{
CanTeleport = false;
}
}
}
I have some ideas, i will try them right now, Thank you, even though i knew about DontDestroyOnLoad i didn't think about an empty gameobject that will store the state of my game, Thank you! Edit: i'm always having trouble accessing another script's variables, can you tell me what is the best way? for now i just do this: GameObject.Find("Game Status").GetComponent<GameState>().lastportal = PortalName;
Hey, i did it! Thank you so much. Problem Solved :P
Answer by xxmariofer · Jun 30, 2019 at 06:54 PM
add a spawnscript to each portal, the only thing you need in there is a collisionenter event, and compare the collider of the object that has just collided. you can have a public transform variable in the spawnscript for handleling where do you want to spawn respawn the object. there are hundreds of fancier ways of doing this but this one is the easiest one
Yeah, but how can i know from which portal the player just came from? Because if he came from scene 1 he needs to spawn in portal 1, and if he came from scene 2, he needs to teleport in portal 2. There are two spawnpoints that the player can spawn in.
You could use a Static variable on an object that persists across scenes, or possibly use PlayerPrefs to save a value temporarily so on the other side you can check where the player came from and run the according code
From portal 2 you can only go to spawn point a, or i am wrong?
From portal 2 you can go to scene 2, and from scene 2 you can go to portal 2 that is located in a different scene. if it's hard to understand, if you know by any chance the game $$anonymous$$apleStory, i want something like that.
Your answer
Follow this Question
Related Questions
Level design clone of King of Thieves 0 Answers
Softbody physics with 2D sprites 1 Answer
How to blur 2D background sprites? 0 Answers
Timer jitters? SOLVED 0 Answers