Hi, I making a platform game that when the player jump on the platform after a few second the platform spriterender and collision is off and after few second after that the platform spriterender and collision is on again.
this is my code for the platform
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platform : MonoBehaviour
{
BoxCollider2D box2D;
SpriteRenderer platformSp;
public float timeToSpawn;
public bool platformDown;
// Start is called before the first frame update
void Start()
{
box2D = GetComponent<BoxCollider2D>();
platformSp = GetComponent<SpriteRenderer>();
}
private void Update()
{
if(platformDown)
{
platformSp.enabled = true;
box2D.enabled = true;
}
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Player") && !platformDown)
{
StartCoroutine(PlatformFall());
platformDown = true;
}
else if(platformDown)
{
StartCoroutine(PlatformSpawn());
}
}
IEnumerator PlatformFall()
{
yield return new WaitForSeconds(1f);
platformSp.enabled = false;
box2D.enabled = false;
}
IEnumerator PlatformSpawn()
{
yield return new WaitForSeconds(timeToSpawn);
platformDown = false;
}
}
there no error and I dont know what to do I appreciate any help
Yes I using the same platform gameobject because if I destroy the platform I encounter a few more problem that why I choose to this instead
– zeref_slayerAnd thank you for the answer I will give it try
– zeref_slayer