Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FelixTheWolfrick · Feb 09, 2020 at 06:11 PM · 2dcollisiontriggerdestroyplatformer

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();
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tripleganger · Feb 09, 2020 at 06:26 PM 0
Share

$$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.

avatar image FelixTheWolfrick · Feb 09, 2020 at 06:45 PM 0
Share

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"));
         }
     }
avatar image
0

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"));
         }
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tripleganger · Feb 09, 2020 at 06:53 PM 0
Share
       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?

avatar image Tripleganger · Feb 10, 2020 at 02:07 AM 0
Share

@FelixTheWolfrick See my comment in this thread

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

300 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges