I am a trying to recreate the first stage of Super Mario Brothers, but I am having a bit of trouble with the collision system between Koopa Shells. I have written two different scripts in hopes to achieve this:
This First script is to handle the Basic movement and start shell movement on Player Collision
It’s Rather long so I took the liberty of including only parts that were in direct correlation/ causation of the problem
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;
}
}
The Second Script handles all other collision and inherits from the first in an attempt to simplify the calling from script to script… Could also be the cause of my problem.
using UnityEngine;
using System.Collections;
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);
}
}
}
** Currently the script above does not destroy the various gameobjects under the Enemy and Player tag. Maybe this could be done implicitly with an associated script to the desired gameobject that correlates with the tag…**
** Also the script destroys both shells during a collision. I don’t understand why my various boolean flags aren’t creating the desired result(s):**
- If both shells are moving then both
shells are destroyed. - If only one shell is moving then
the resting shell is destroyed.
I am seeking to understand the error in my logic for this to work as
desired.
Thank you for your time!