using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HappyBlock : MonoBehaviour
{
[SerializeField] private GameObject toSpawn;
private BoxCollider2D boxCollider;
private LayerMask playerLayer;
private int numberSpawned;
// Start is called before the first frame update
void Start()
{
boxCollider = GetComponent<BoxCollider2D>();
playerLayer = LayerMask.GetMask("Player");
numberSpawned = 0;
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
print("HappyBlock collided with " + collision.gameObject.name);
bool cond = numberSpawned == 0 && collision.gameObject.layer == LayerMask.NameToLayer("Player") && collision.GetContact(0).normal.y > 0;
if (cond)
{
//set BlockHit trigger
GetComponent<Animator>().SetTrigger("BlockHit");
//spawn the object above
Instantiate(toSpawn, transform.position + new Vector3(0, 1, 0), Quaternion.identity);
numberSpawned++;
}
}
}
Both player and block have a 2d collider, and the player has a rigid body, but for the OnCollisionEnter2D to be recognized and actually do something I have to jump several times, even though the collider of the block still stops the jumping player. How can I fix this?