我试图重建超级马里奥兄弟的第一阶段,但是我在Koopa Shells之间的碰撞系统上遇到了一些麻烦。 我写了两个不同的脚本,希望达到这个目的:
这第一个脚本是处理基本运动,并开始在球员碰撞壳运动
这是相当长的,所以我冒昧地只包括与问题直接相关/因果关系的部分
public class KoopaShell : MonoBehaviour { //Makes this accessible to other Scripts public bool Moving; // Boolean Flags to be inherited bool shellLeft, shellRight, rightBlocked, leftBlocked; public float rayDis; public LayerMask enemyMask; public Transform rCast; [... Rest of the code..] void OnCollisionEnter2D(Collision2D coll) { if (leftBlocked || rightBlocked) // If the either side of the shell is hit if (coll.gameObject.tag == "Player" && Moving == false) // If the player makes contact with a resting shell { MoveShell(); } } public IEnumerator MoveShell() //Courotine for 2D Collision { //The Courotine will wait for the end of the frame yield return new WaitForFixedUpdate(); //Then set moving to true Moving = true; } }
第二个脚本处理所有其他冲突,并从第一个inheritance,试图简化从脚本到脚本的调用….也可能是我的问题的原因。
public class MovingShellScript : KoopaShell { //This is a continuation of the KoopaShell Script //Dealing with 2D Collision void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "Koopa Shell") // if contact is made with another shell that is moving if(col.gameObject.Equals(Moving) == false) //if the shell is not moving { { Destroy(col.gameObject); // Then the resting shell is destroyed Debug.Log("Looks like another shell is unemployed"); } } if (col.gameObject.tag == "Enemy") // If a Enemy is hit by the Shell if (Moving == true) // and If the Shell is moving { // The GameObject hit is Destroyed Destroy(col.gameObject); } } }
目前上面的脚本不会破坏“敌人”和“玩家”标签下的各种游戏对象。 也许这可以隐式地完成一个相关的脚本到所需的游戏对象,与标签相关….
脚本也在碰撞过程中破坏了两个炮弹。 我不明白为什么我的各种布尔标志不创造所需的结果(S)
如果两个炮弹都在移动,那么两个炮弹都被毁坏。
如果只有一个shell正在移动,那么搁置的shell就会被破坏。
我正在寻求理解我的逻辑错误,以便按照要求工作。
感谢您的时间!
我认为你可能误解了Equals()方法的作用 – 它检查col.gameObject
实例col.gameObject
是否等于你传入的参数。 你传递一个Boolean
值,这不是一个GameObject
,所以结果总是假的。
我想你的意思是写更多这样的东西:
void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.CompareTag("Koopa Shell")) // if contact is made with another shell { // Get a reference to the other shell: KoopaShell otherShell = col.gameObject.GetComponent<KoopaShell>(); if(otherShell.Moving == false) //if the other shell is not moving { Destroy(col.gameObject); // Then the resting shell is destroyed Debug.Log("Looks like another shell is unemployed"); } } ...
首先,我的dynamic布尔variables是一个静态variables(在我问这个问题之前就已经改变了)。 其次,我没有引用我正在碰撞的对象,从而建立在我的第一个逻辑错误的静态布尔 – 这使得所有的实例共享相同的值。 最后,在意识到后两者之后,以及我的代码不能正确地允许整个引擎访问移动布尔值的事实 – 我慷慨地指出要检查这个值,然后当我意识到我过早地不使用StartCoruotine()来调用我的昆虫。
IEnumerator MoveShell() //Courotine for 2D Collision { //The Courotine will wait for the end of the frame yield return new WaitForSeconds(0.001f); //Then set moving to true yield return Moving = true; } void OnCollisionEnter2D(Collision2D coll) { // Any collision that deals with the Player will be here if (leftBlocked || rightBlocked) // If the either side of the shell is hit if (coll.gameObject.tag == "Player" && this.Moving == false) // If the player makes contact with a resting shell { StartCoroutine(MoveShell()); // Calls our Courotine } // Any collision that deals with other Koopa Shells will be here if (coll.gameObject.CompareTag("Koopa Shell")) // if contact is made with another shell { // Get a reference to the other shell: KoopaShell otherShell = coll.gameObject.GetComponent<KoopaShell>(); if (otherShell.Moving == false && this.Moving == true) //if one of the shells is at rest { Destroy(coll.gameObject); // Then the resting shell is destroyed Debug.Log("The Shell at rest was destroyed"); } if(otherShell.Moving == true && this.Moving == true)// if both shells are not moving { Destroy(this.gameObject); //Destroy the moving shell that this script is attached to Destroy(coll.gameObject); //Destroy the moving shell that is being collided with Debug.Log("Both Shells involved were destroyed"); } } }
实际上代码是没有在这个分裂的地方。 请原谅介绍失败的尝试。
再次感谢您的所有关注或以任何方式协助我! 🙂