I’m trying to make a 2.5D platformer and I wanted a platform that would break a few seconds after the player landed on it, so these are the scripts I wrote:
Code for the platform.
public class brokenPlatform : MonoBehaviour
{
public float holdTime = 5f;
public bool stoodOn = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(holdTime == 0)
{
Destroy(gameObject);
}
while(stoodOn == true)
{
holdTime -= 1f;
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
stoodOn = true;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
stoodOn = false;
holdTime = 5f;
}
}
}
Code for the player.
public class playerControl : MonoBehaviour
{
public float speed = 6f;
public float jumpForce = 5;
public bool grounded = true;
private float horbutt;
private Rigidbody playerRB;
// Start is called before the first frame update
void Start()
{
playerRB = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
horbutt = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * Time.deltaTime * speed * horbutt);
if (Input.GetKeyDown(KeyCode.W) && grounded)
{
playerRB.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
grounded = false;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("ground") || collision.gameObject.CompareTag("brokenPlat"))
{
grounded = true;
speed = 6f;
}
if (collision.gameObject.CompareTag("goo"))
{
grounded = false;
speed = 2f;
}
}
}
But when I tested it, unity crashed as soon as the player landed on it.
What do I do?