0
\$\begingroup\$

I have the main scene "camp", in camp I have two scene teleporters, "cave" and "tent", each teleport into a different scene. The issue I am having is that when I exit cave and tent, they both get teleported onto camp at the same spot. I want to be able to specify which spot they get teleported to separately. Thank you.

Edit: added player movement script, with old attempt at fixing issue, added full scene loader script with new attempt. each attempt does not included the other.

Edit2: Temporary solution, if you move your teleporters right where you want your character to go, you can manipulate it in a way that doesn't require a code with character location.

/* ONCOLLISIONENTER WAS A NEW ATTEMPT AT TRYING TO FIX IT,
   I PLACED THIS IN THE LOALDLEVEL SCRIPT */

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

public class LoadLevel : MonoBehaviour
{
    public int iLeveltoLoad;
    public string sLeveltoLoad;
    private GameObject player;

    public bool useIntToLL = false;
   
   
    private void OnTriggerEnter2D(Collider2D collision)
    {
        GameObject colgameObject = collision.gameObject;
        
        if(colgameObject.name == "Player")
        {
            LoadScene();
            //playerstore.initalValue = playerPos;
            
        } 
     
    }

    void LoadScene()
    {
        if(useIntToLL)
        {
            SceneManager.LoadScene(iLeveltoLoad);
        }
        else
        {
            SceneManager.LoadScene(sLeveltoLoad);
        }
    }

/* NEWER ATTEMPT AT FIXING ISSUE */
                           
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Cave exit lvl")
        {
            player.transform.position = new Vector3(-1.12f, 2.7f, 0f);
        }

        if (collision.gameObject.name == "ExitTent")
        {
            player.transform.position = new Vector3(4.65f, -2.44f, 0f);
        }
    }
}

/* MY FIRST ATTEMPT AT FIXING THE CODE WAS IN PLAYERMOVEMENT, "FINDSTARTPOS" */

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody2D body;
    public float playerSpeed = 5f;
    public Animator anime;
    Vector2 movement;
    private GameObject player;
    private GameObject[] players;
    private bool cave = false;
    private bool tent = false;
    private Collision collision;

    private void Awake()
    {
        anime = GetComponent<Animator>();   
    }
    private void Update()
    {
        DontDestroyOnLoad(gameObject);
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");
      

        anime.SetFloat("Horizontal",movement.x);
        anime.SetFloat("Vertical", movement.y);
        anime.SetFloat("Speed", movement.sqrMagnitude);
    }

    private void OnLevelWasLoaded(int level)
    {
        FindStartPos();
        
        
      
        players = GameObject.FindGameObjectsWithTag("Player");

        if(players.Length > 1)
        {
            Destroy(players[1]);
        }
    }


    private void FixedUpdate()
    {
        body.MovePosition(body.position + movement * playerSpeed * Time.fixedDeltaTime);
    } 

/* OLD ATTEMPT AT FIXING ISSUE 
   ↓                         ↓ */
    void FindStartPos()
    {
        
        if (GameObject.FindWithTag("Cave"))
        {
            player.transform.position = new Vector3(-1.12f, 2.7f, 0f);
        }

        if (GameObject.FindWithTag("Tent"))
        {
            player.transform.position = new Vector3(4.65f, -2.44f, 0f);
        }
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ You may be interested in this devlog about linking together scene entrances and exits in Astortion. \$\endgroup\$
    – DMGregory
    May 22 at 16:01
  • \$\begingroup\$ For the first attempt, both tags "Cave" and "Tent" were found on the same original scene, so when it was looking for the game tag, it just chose the first if statement, this just made the player spawn on the same spot when exiting both scenes. For the second attempt I dont think the player.transform.position is working, when entering the cave it keeps exiting and entering the scene, when you exit the tent, it send you to the position where the character starts when you start running the program. \$\endgroup\$
    – Dinosora
    May 22 at 16:42
  • \$\begingroup\$ Its different from the behavior I want since it doesn't spawn in the position that I want or the player transform position doesn't work \$\endgroup\$
    – Dinosora
    May 22 at 16:44
  • \$\begingroup\$ You may also be interested in this previous Q&A about picking a spawn point in the next scene to load. \$\endgroup\$
    – DMGregory
    May 22 at 20:54

0

You must log in to answer this question.

Browse other questions tagged .