简体   繁体   English

玩家不移动到请求的 position - unity 2D

[英]player not moving to requested position - unity 2D

my goal is when in radius of a door, when you press down you move scene and get moved to the position of the door but no matter what i try, my player wont move there.我的目标是在门的半径范围内,当你按下你移动场景并移动到门的 position 但无论我尝试什么,我的播放器都不会移动到那里。 any ideas?有任何想法吗?

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

            }

        }
    }
}

i dont really know what more to say but it keeps saying my question requires more info so thank you for reading my question:)我真的不知道还能说什么,但它一直在说我的问题需要更多信息,所以感谢您阅读我的问题:)

Hmm... shouldn't you call SceneManager.Loadscene after moving the player?嗯...你不应该在移动播放器后调用 SceneManager.Loadscene 吗? Try that first..先试试那个..

If you are opening a new scene, the current player object is destroyed along with the previous scene.如果是开新场景,则当前玩家object连同上一个场景一起被销毁。 If you want to persist it, you can use DontDestroyOnLoad如果你想持久化它,你可以使用DontDestroyOnLoad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM