I’m working on a 2d platformer for my Game Prod I class and I’m having an issue with collisions. I’m trying to make it so when the player runs into an item it destroys the item but nothing I try has worked. The player and the item has rigidbody 2d and collider 2d with trigger on. The item has a tag called ingredient and the player has a tag called Player. I’ve tried OnTriggerEnter and OnCollisionEnter and neither has worked and I have no idea why.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//Variables
//Movement
public float speed;
public float jumpForce;
private float moveInput;
private Rigidbody2D rb;
//Platforms
private bool grounded;
//Jumping
private int jumps;
public int jumpsValue;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
jumps = jumpsValue;
}
//Check if hitting ingredient
void OnTriggerEnter(Collider other)
{
Debug.Log("Test Trigger");
if (other.tag == "Ingredient")
{
Destroy(other);
}
}
//Jump
void Jump()
{
//Check if on platform & reset jumps
if (grounded)
{
jumps = jumpsValue;
}
//Jump
if (Input.GetKeyDown(KeyCode.W))
{
rb.velocity = Vector2.up * jumpForce;
jumps--;
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
rb.velocity = Vector2.up * jumpForce;
Debug.Log("Needed");
}
}
// Update is called once per frame
void Update()
{
//Moving Input
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
//Jump
Jump();
}
}