Answer the question
In order to leave comments, you need to log in
Why does the game freeze on OnCollisionEnter?
public class Spawning : MonoBehaviour
{
public bool Isspawn;
public GameObject prefabToSpawn;
public Transform left;
public Transform right;
public Rigidbody2D rigidbody2D;
private void Awake()
{
rigidbody2D = gameObject.GetComponent<Rigidbody2D>();
}
private void Start()
{
StartCoroutine(spawn());
}
private IEnumerator spawn()
{
while (true)
{
if (Isspawn)
{
float timeRand = Random.Range(0.5f, 1f);
yield return new WaitForSeconds(timeRand);
float randomPos = Random.Range(left.position.x, right.position.x);
Instantiate(prefabToSpawn, new Vector3(randomPos, left.position.y, 0), Quaternion.identity);
}
}
}
private void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.CompareTag("Player"))
Isspawn = true;
rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
}
private void OnCollisionExit2D(Collision2D coll)
{
if (coll.gameObject.CompareTag("Player"))
Isspawn = false;
rigidbody2D.bodyType = RigidbodyType2D.Static;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question