C# Can';更换播放机后不要移动';s位置

C# Can';更换播放机后不要移动';s位置,c#,unity3d,C#,Unity3d,我想做一个心灵传送。玩家点击传送门,然后转到另一个传送门。然而,当他到达那里时,他无法移动 // Update is called once per frame void Update() { if(hitted1){ posi = new Vector3(42.49f, 0.5f, 163.8f); player.transform.position = posi; //hitted1 = false; } } void On

我想做一个心灵传送。玩家点击传送门,然后转到另一个传送门。然而,当他到达那里时,他无法移动

// Update is called once per frame
void Update()
{
    if(hitted1){
        posi = new Vector3(42.49f, 0.5f, 163.8f); 
        player.transform.position = posi;
        //hitted1 = false;
    }
}

void OnTriggerEnter(Collider other){
    if(other.name == "FPSController"){
        Debug.Log("player hit tele1");
        hitted1 = true;
    }
}

这是你所问问题的答案。你的代码不断地一遍又一遍地调用同一个传送代码,因此它会在第一次移动角色,但在每一帧的初始更新后,会一直将角色放在相同的位置。在你的评论中,你说un注释hitted1=false;导致其他问题,这可能是真的。但这将解决运动问题。解决这个问题,然后解决由于取消注释而产生的问题

// Update is called once per frame
void Update()
{
    if(hitted1){
        posi = new Vector3(42.49f, 0.5f, 163.8f); 
        player.transform.position = posi;
        hitted1 = false;
    }
}

void OnTriggerEnter(Collider other){
    if(other.name == "FPSController"){
        Debug.Log("player hit tele1");
        hitted1 = true;
    }
}

看起来您正在使用标准资产中的FPS控制器。如果为true,则FPSController对象包含CharacterController组件。此组件可能会阻止通过
transform.position
更改对象的位置(换句话说,位置将更改为特定位置,但在此之后,对象将返回到更改
transform.position
之前设置的上一个位置)

如您所述,FPSController对象在被远程传送到新位置后刚刚冻结。这是因为当
hitted1
的值变为
true
时,
player.transform.position=posi运行每个
Update()
原因
hitted1
值保持为
true

但是,当您尝试取消注释
hitter1=false
时,FPSController对象将返回到其以前的位置。这是因为CharacterController在通过变换位置更改位置时保持启用状态

尝试:

CharacterController controller;


    void Start() {
    controller = player.GetComponent<CharacterController>();
    }  
    void Update()
    {
        if(hitted1){
            posi = new Vector3(42.49f, 0.5f, 163.8f);
            controller.enabled = false;
            player.transform.position = posi;
            controller.enabled = true;
            hitted1 = false;
        }
    }
字符控制器;
void Start(){
controller=player.GetComponent();
}  
无效更新()
{
如果(hitted1){
posi=新矢量3(42.49f,0.5f,163.8f);
controller.enabled=false;
player.transform.position=posi;
controller.enabled=true;
hitted1=假;
}
}

您是否有意注释掉hitted1=false?如果没有,当它未注释时会发生什么?蔡斯:当未注释时会出现另一个问题,玩家会回到原来的位置…@gustavo302解释另一个问题如果不注释该行会导致另一个问题,那么你在其他地方有问题。因为你的代码阻止玩家移动,因为一旦玩家传送,你会不断设置它的位置。我们无法解决原始问题,因为您没有包含其他问题的详细信息。@gustavo302玩家如何移动?我怀疑这段代码是导致玩家在传送后向后移动的原因。如果没有看到代码,我无法确定。