如何停止门户A和B之间的无限旅行? [英] How to stop an infinite travel between portal A and B?

查看:92
本文介绍了如何停止门户A和B之间的无限旅行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与unity一起开发2D游戏,并且我试图添加像黑洞之类的门户系统,当玩家进入黑洞A时,他会被传送到黑洞B,反之亦然……

I'am Working on a 2D game with unity , and I am trying to add a portal system like blackholes, when the player enter Blackhole A he get teleported to blackhole B and vice versa ...

主要问题是,当玩家目标位置更改为黑洞"B"时,由于黑洞B本身就是一个传送门,因此玩家陷入了A和B之间的无限行进,从而导致许多错误和非常糟糕的行为.

The main issue is that when the player object position get changed to blackhole "B", since blackhole B is a portal itself, the player get stucked in a infinite travel between A and B leading to many bug and a very bad behaviour .

我想要的是能够从门户A移至B并从门户B移至A.在A&之间的无限旅行中不会陷入困境B

What I want is to be able to move from portal A to B and move back from portal B to A . Without getting stuck in a infinit travel between A & B

这里是负责的代码:

public GameObject exitBL ;
    public bool teleport = false;
    public Collider2D coll;
            // Use this for initialization
            void Start ()
            {

    }

            // Update is called once per frame
            void Update ()
    {
            if (teleport) {
                    exitBL.GetComponent<BlackHolePortal>().enabled = false; // Disable the logic for portal B which is exitBl
                    goToBl(coll);
                            }

    }
    void OnTriggerEnter2D(Collider2D col){
            if (col.tag == "Player") {
                    coll = col;
                    //col.transform.position = new Vector3 (exitBL.transform.position.x  , exitBL.transform.position.y , 0f);
                    teleport = true;


                            }

    }
    void OnTriggerExit2D(Collider2D col){
            if (col.tag == "Player") {
                    exitBL.GetComponent<BlackHolePortal>().enabled = true;
                            }

            }
    void goToBl(Collider2D col){
            teleport = false;
             col.transform.position = new Vector3 (exitBL.transform.position.x + 1.0f  , exitBL.transform.position.y + 1.0f  , 0f);

            coll.rigidbody2D.AddForce (new Vector2 (-1f,2f));


    }

即使我要添加 AddForce()来快速从Portal B移走,照顾玩家将pos移到Portal pos的那条线也会以无限的方式运行,因为玩家会移到也是门户的门户B上,以便玩家回到门户A..etc.

Even If I'am adding AddForce() to move from the Portal B quickly, the line which take care of moving the player pos to the portal pos run in an infinite way since the player will move to a portal B which is a portal too, so the player get back to portal A..etc .

有什么办法可以阻止这种行为?我需要逻辑上的帮助.

Is there any way to stop this behaviour ? I need some help in the logic .

编辑:我已经更改了代码:现在,当它变为隐形时,我将禁用逻辑.但是我遇到了同样的问题.

Edit : I've changed the code : Now I'am disabling the logic when it get to teleported . But i get the same issue.

**注意**:现在,我正在使对撞机非常小,并传送玩家的球并增加偏移量,此解决方案有效,但是应该有更好的方法.

**Note ** : Right now, I am making my collider very small and teleporting my player and adding an offset, this solution work, but there should be a better way .

这是我已经做过的一些事情,这些事情不起作用或部分起作用:

Here some stuff I've done and which doesn't work or work partially :

  1. 使用一个称为Teleport的布尔变量,此变量在进入触发对撞机时会获得真值,并在Update中检查该变量,如果为true,请执行 goToBl()(更新运行每帧一次)(无效)

  1. Using a bool var called teleport, this one get a true value when it enter the trigger collider, and check for that var in Update, if true do goToBl() (Update run once per frame ) ( doesn't work)

  1. 使用相同的布尔值,并在Update()中检查其是否正确,然后禁用门户网站B的脚本;直到肋骨脱离.(无效).

  1. Using the same bool and check if it's true in the Update (), then disable the script for portal B; until the pleyr get out of it. (doesn't work) .

使黑洞门户对撞机很小,然后移至门户位置+ 1;此解决方案不是最终解决方案(只是一种解决方法),它可以部分起作用,有时它可以很好地工作,有时却不起作用.

Making black hole portal colliders very small and then move to the portal position + 1; This solution is not a final one (just a workaround), it works partially, sometimes it works very good, sometimes it doesn't work .

有人可以建议我解决该问题的方法吗?

Can any one suggest me a solution to solve this problem ?

谢谢!

推荐答案

您最好的选择是让玩家在门户的位置> +1 (在x轴或y轴上),以便它们实际上是在门户网站前面传送的.

Your best bet is to have the player position >+1 (in the x or y axis) of the portal so that they are actually teleported in front of the portal...

但是,如果出于任何原因,您希望他们直接传送到门户:在每次传送之后,玩家都必须再次传送,直到他们离开传送器位置为止.

But, if for whatever reason you want them to teleport directly to the portal: after each teleport don't allow then player to teleport again until they have walked away from the teleportor position.

这可以通过使用 events 和名为 canTeleport 的简单 bool 来完成.

This can be done using events and a simple bool named canTeleport.

这篇关于如何停止门户A和B之间的无限旅行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆