我的目标是当你在门的半径内,当你按下你移动场景并移动到门的位置,但无论我怎么尝试,我的玩家都不会移动到那里。什么好主意吗?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Door : MonoBehaviour
{
public string sceneToWarpTo;
public string doorScene;
public float doorLocationX;
public float doorLocationY;
public GameObject player;
private float x;
private float y;
// Start is called before the first frame update
void Start()
{
x = player.transform.position.x;
y = player.transform.position.y;
}
// Update is called once per frame
void Update()
{
}
IEnumerator LoadFixThing()
{
//yield on a new YieldInstruction that waits for 5 seconds.
yield return new WaitForSeconds(1);
}
public void OnTriggerStay2D(Collider2D coll) {
if (coll.tag == "Player"){
if(Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)){
Debug.Log("Enter Door");
Debug.Log("warping to " + sceneToWarpTo + " from " + doorScene);
SceneManager.LoadScene(sceneToWarpTo);
LoadFixThing();
player.transform.position = new Vector2(doorLocationX, doorLocationY);
Debug.Log("player pos: " + player.transform.position);
Debug.Log("door pos: " + this.transform.position);
Debug.Log(player.name);
}
}
}
}
我真的不知道还有什么要说的,但它一直在说我的问题需要更多的信息,所以谢谢你阅读我的问题:)
嗯…你不应该调用SceneManager。移动玩家后加载场景?
如果你打开一个新的场景,当前的玩家对象会随着之前的场景一起被销毁。如果你想持久化它,你可以使用DontDestroyOnLoad