Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How do I customize where my character spawns?

Discussion in '2D' started by Zorafin, Feb 17, 2019.

  1. Zorafin

    Zorafin

    Joined:
    Oct 10, 2017
    Posts:
    16
    Hi, beginning Unity user again. I'm trying to build a level where entering a room from the left spawns you at the room's left entrance, but entering it from the right makes me spawn at the right entrance. But it seems like the starting position for the player is not customizable.

    Is there no way to just say I want my character to spawn at this location?
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    what project are you using? in general in any section where you 'enter a room' is called, you would add another call to set your player position, however you keep track of it. determining whether you came in from left or right can be done in many ways too, again depends on your project. post screenshots and relevant code if possible
     
  3. Zorafin

    Zorafin

    Joined:
    Oct 10, 2017
    Posts:
    16


    That's what I have so far. I have door objects (no visuals on them yet) where you press up on them, and you enter into whatever room they lead to.

    The code attached to them is as follows:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class room1Handler : MonoBehaviour {

    //public GameObject player;
    public Vector3 entrance;
    public string exit;
    public GameObject player;
    public Vector3 position;

    // Use this for initialization
    void Start () {
    entrance = new Vector3(4, 4, 0);
    }

    // Update is called once per frame
    void Update () {

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
    if (Input.GetAxis("Vertical") >0){
    SceneManager.LoadScene(exit);
    }
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
    if (Input.GetAxis("Vertical") > 0)
    {
    SceneManager.LoadScene(exit);
    }
    }

    }


    I'm not doing much more than listening for an Up input upon collision and loading the next scene. And, my code isn't optimized, so there's probably some mess. I think it's small enough to be readable though.
    If I could just pass along a coordinate and set my player's position to it, then I'd have everything I need for this part. But I'm not sure if that's possible.
     
  4. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    i'm not too familiar with loadscene so some of the stuff i say may not work. also not sure if this handler you've posted is duplicated for each exit or is for the entire room etc.

    i think you'll want to add an exitEntrance vector, that determines what you character position should be after you exit this room.

    but even more than that, you'll want to ensure you code is reusable and consistent. you need to make sure that your door/exit code exists only once, and your actual configurations are done per exit. once you do that, you shouldn't have any issues determining where the character spawns/moves to after the new scene loads.

    alternatively to the exitEntrance vector, you can have an exitNumber (int) and have predefined entrance numbers on the new scene and move accordingly. this is a bit more self contained but both work fine.

    so in general the part you'll add will be straight after LoadScene, something like:
    SceneManager.LoadScene(exit);
    NewScene.Player.Position = exitEntrance;
     
  5. Zorafin

    Zorafin

    Joined:
    Oct 10, 2017
    Posts:
    16
    Yeah, this code is just dumped on any door object. It's the same code for every door, so all that would really change is what scene they take you to and what coordinates they dump you at.

    I tried your psudocode, but I don't know what to make of NewScene. I didn't find anything like that on a google search. I'm also not sure any code will run after LoadScene. Is that something I need to set up beforehand?