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);
}
}
}