- Home /
Collision not working for 2d platformer
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();
}
}
Answer by Tripleganger · Feb 09, 2020 at 06:22 PM
//Check if hitting ingredient
void OnTriggerEnter(Collider other)
{
Debug.Log("Test Trigger");
if (other.tag == "Ingredient")
{
Destroy(other.gameObject);
}
}
Remember that "other" is the Collider, not the object. You were destroying the collider. Also, check that the tag is "Ingredient" and not "ingredient" as you wrote because tags are case-sensitive.
$$anonymous$$gestions to improve the code:
1 - Ins$$anonymous$$d of using GetComponent, which is incredibly resourceful, just assign your variables through the inspector by making it public.
2 - Remember to multiply the Vector2 velocity with Time.deltaTime, otherwise your character will move slowly when your FPS drop, and very quickly when you have a high FPS ratio.
3 - Use Compare.Tag("NameOfTag") to compare taga, ins$$anonymous$$d of other.tag == "NameOfTag". When Unity provides you with built-in methods, it's best to use them.
Thanks for the tips, the collision still isn't working though. Like I said before the Debug isn't even getting triggered. Did you want me to use Compare.Tag("Ingredient") inside the if([here])? Cuz when I tried it just gave me an error.
//Check if hitting ingredient
void OnTriggerEnter(Collider other)
{
Debug.Log("Test Trigger");
if (other.tag == "Ingredient")
{
Destroy(GameObject.FindWithTag("Ingredient"));
}
}
Answer by FelixTheWolfrick · Feb 09, 2020 at 06:27 PM
@Tripleganger The tag is correct and I updated the Destroy but it still doesn't work. It's not even triggering since the debug isn't going off.
//Check if hitting ingredient
void OnTriggerEnter(Collider other)
{
Debug.Log("Test Trigger");
if (other.tag == "Ingredient")
{
Destroy(GameObject.FindWithTag("Ingredient"));
}
}
void OnTriggerEnter(Collider other)
{
Debug.Log("Test Trigger");
if (other.CompareTag("Ingredient"))
{
Destroy(other.gameObject);
}
}
Use the one above, I have just tried it and it works. Silly question, but... Have you attached the script to the player?
Your answer
Follow this Question
Related Questions
2d game end level with trigger and colission with trigger who to make a if condition 1 Answer
How can I detect a collision point, but allow player to pass through collider. 1 Answer
Why does my OnCollisionEnter2D not work? 3 Answers
Rigidbody2D.velocity out of controll 3 Answers
How to destroy only one GameObject??? 0 Answers