I had created a falling platforms using this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Brick : MonoBehaviour {
Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.name.Equals ("Chara_03")) {
Invoke ("DropPlatform", 0.6f);
Destroy (gameObject, 2f);
}
}
void DropPlatform()
{
rb.isKinematic = false;
}
}
Everything was fine and I kept creating more sections of the game. As I went back to test the game again, I noticed now the falling platforms have an error. I do not know what changed. All I did was move the created platform sprites into a sub folder (the side bar under “Hierarchy”) to clean up the space as it was starting to get messy.
It seems like the gravity glitches and the character goes flying, and all the platforms fall. I have a box collider on the platforms, and Rigidbody 2D (kinematic, simulated checked, continuous, start awake). Gravity scale is set to 2 under Dynamic if it matters, but I leave it on Kinematic.
Update: I figured it out! Moving all the platforms under one main “Platform” folder, or so I thought was just a folder, actually turned out to be a platform itself - So it linked all the platforms together in a sense. At least, that’s what I’m understanding as to what happened. I fixed it by moving the platforms all out and now I don’t have a problem.
However, I am realizing if the player fails at this point in the game, the platforms do not reappear. Also, since I have it so that the platforms fall based on collision, sometimes when the player bumps the side of the platform, they will fall. I am trying to figure out a way to make it so that only when the player is on top of the platform, they will fall.
Any tips on the two points above (re-spawn platforms after X seconds, and platform fall from input only above) would be greatly appreciated.
you can add an other box collider 2d on the top from the platform, change it to trigger and check it in the script
void OnTriggerEnter2D (Collider2D col)
I’m assuming your platforms are a prefab, no?
You could add an Empty Game object to the platform as long as it isn’t a child object of the platform itself. The empty will follow the platform down if you make it a child. You want it to be a stationary Transform.position to respawn to. This will work as long as you do not Destroy() your platforms(If you Destroy() your respawn point will also die). Instead, set up a collider below and make it respawn to it’s original point upon OnTriggerEnter2D or OncollisionEnter2D.