- Home /
Question by
cenkbozkurt05 · Jun 11, 2021 at 08:47 PM ·
scripting problempositionscript.unity 2dtransform.position
How can i change position of my character
Hi, I'm making a 2d game, my problem is that as you can see in Picture 1, I have a house and there is a trigger box where I show it with a red circle, trigger box save my position and changing scene when i press E.
As you can see in Picture2 there is a door on the left side and when i press E to the door i'm return to my old scene and this time i want to start to the left side where i showed it with a yellow circle in the Picture 1 but somehow my code that i wrote doesn't work i tried everything that i could i really need some help i'm pretty new in C# (Sorry for my bad eng.)
Picture 1 https://prnt.sc/153gv7a Picture 2 https://prnt.sc/153gtlx
myPlayerCode =
public bool NewOutsideTransform;
void Start()
{
//RigidBody2D'yi rgb2 diye göster.
rgb2 = GetComponent<Rigidbody2D>();
FireHomein.GetComponent<FireHomein>().NewOutsideTransform = (PlayerPrefs.GetInt("Transform1") != 0);
Time.timeScale = 1;
//Karakter konumu varmı?
if (PlayerPrefs.HasKey("Player1"))
{
float x = PlayerPrefs.GetFloat("Player1");
float y = PlayerPrefs.GetFloat("Player2");
float z = PlayerPrefs.GetFloat("Player3");
transform.position = new Vector3(x, y, z);
Debug.Log("Karakter Pozisyonu Alındı");
}
else
{
if(NewOutsideTransform == true)
{
transform.localPosition = new Vector3(FireinHomeOutPosition.position.x, FireinHomeOutPosition.position.y, 0);
}
else
{
transform.localPosition = new Vector3(StartPos.position.x, StartPos.position.y, 0);
}
Debug.Log("Karakter Pozisyonu Yok");
}
}
FireHomeinCode =
public class FireHomein : MonoBehaviour
{
public bool isInRange = false;
public GameObject Player;
public bool NewOutsideTransform = false;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
PlayerPrefs.SetFloat("Player1", transform.position.x);
PlayerPrefs.SetFloat("Player2", transform.position.y);
PlayerPrefs.SetFloat("Player3", transform.position.z);
isInRange = true;
NewOutsideTransform = true;
PlayerPrefs.SetInt("Transform1", (NewOutsideTransform ? 1 : 0));
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
PlayerPrefs.DeleteAll();
isInRange = false;
NewOutsideTransform = false;
PlayerPrefs.SetInt("Transform1", (NewOutsideTransform ? 1 : 0));
}
}
private void Update()
{
if(isInRange == true)
{
if (Input.GetKeyDown(KeyCode.E))
{
SceneManager.LoadScene("FireHomein");
}
}
}
}
Comment