简体   繁体   English

一次OnCollisionStay在C#中统一检测两个平面的碰撞?

[英]OnCollisionStay at a time Collision detection for two plane in C# unity?

I am a beginner and working in Stacker game and there are multiple planes, but one of them is true and other is false. 我是一个初学者,正在从事Stacker游戏,并且有多个平面,但是其中一个是正确的,而另一个是错误的。 Now my problem is that, sometimes stacker stops at two plane simultaneously and both function gets called on 'is game over' and other 'go to next level'. 现在我的问题是,有时堆垛机同时停在两架飞机上,而这两个功能都会在“游戏结束”和其他“进入下一关”时被调用。 Tell me how can I compare two tags, that if both true then do something. 告诉我如何比较两个标签,如果两个标签都为真,那就做什么。 thanks here is my code. 谢谢这里是我的代码。

void OnCollisionStay(Collision collision) {

        if (collision.collider.tag == "plane1") {

            if (script2.rb.velocity.magnitude == 0.0f) {

                collision.collider.gameObject.GetComponent<Renderer> ().material.mainTexture = color1;
                cont++;
                SetCountText ();
                script1.jump1 ();
                StartCoroutine (wait ());
            }

        } else if (collision.collider.tag == "plane2") {
            if (script2.rb.velocity.magnitude == 0.0f) {
                script.jump ();
                Time.timeScale = 0;
            }
            else {
                if (script2.rb.velocity.magnitude == 0.0f) {
                    script.jump ();
                    Time.timeScale = 0;
                }

            }


        }

      if (collision.collider.tag == "plane1" && collision.collider.tag ==    "plane2") {

            script.jump ();
            Time.timeScale = 0;
            Debug.Log("hello");
        }

Your code doesn't entirely match your question, but I gather that the problem is that both the "win" and "lose" condition are triggered by physics, and both are happening in the same frame? 您的代码并不完全符合您的问题,但是我认为问题是“胜利”和“失败”条件都是由物理学触发的,并且都发生在同一帧中?

The most straightforward way to handle that is to have a bool (or better, an enum) to store the state of your game--if the level is over, the collision functions shouldn't do anything. 最简单的处理方法是让布尔(或更好的是一个枚举)存储游戏状态-如果关卡结束,碰撞函数将不执行任何操作。 It does seem like there should be a pretty, elegant way to solve this, but I'm not aware of one. 似乎应该有一种漂亮,优雅的方法来解决此问题,但我不知道有哪一种方法。 Disabling the colliders probably won't work, since both collisions have already occurred by the time your physics callback functions are called. 禁用对撞机可能不起作用,因为在您的物理回调函数被调用时,这两种冲突已经发生。

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

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