So, not exactly within the confines of the lesson itself, but I’ve been using this opportunity to experiment with learning and developing other forms of coding for this project, namely Flying Enemies (basically C&P of regular enemies with 0 gravity and an invisible floating collider that only interacts with them), Parallax Backgrounds, and Rising/Falling platforms. It’s all pretty satisfactory, however I’m running into an issue with the platforms that I’m hoping someone could help me with. When falling, they ignore all other colliders, so I cannot get them to either stop(land on the ground) or reset (which means if you miss a falling platform jump you have to die and start over). I read online that this was due to them being Kinematic objects, and changed them to dynamic with 0 gravity, massive mass, and x/z constraints so they can only fall downwards at the programmed speed. If I set them to interact with the background in project settings, they are forced upwards, resting outside of and on top of the map as soon as I press play, indicating that they are indeed interacting with colliders. However, when I disable the background interaction, they resume their behavior of falling while ignoring every single other collider, despite being dynamic and only unable to interact with the background and water tiles. Here is the code I have so far for my platforms:
public class FallingPlatform : MonoBehaviour
{
[SerializeField] float fallSpeed = -1f;
Rigidbody2D myRigidBody;
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
}
void Update()
{
}
void OnCollisionEnter2D(Collision2D other)
{
Vector2 fallVelocity = new Vector2(0f, fallSpeed);
if(other.gameObject.tag == "Player")
{
myRigidBody.velocity = fallVelocity;
}
}
void OnCollisionEnter2D(Collider2D other)
{
Vector2 stop = new Vector2(0f, 0f);
if(myRigidBody.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
myRigidBody.velocity = stop;
}
}
}
I also tried "other.tag == “Ground” instead of the “IsTouchingLayers” code, with all of the proper layers selected, but that did not work either.
It functions for when the player touches it, and begins moving. I’m just not understanding why it ignores the ground. Any help is greatly appreciated!