Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Firax666 · Feb 17, 2021 at 04:22 PM · 2d game2d-platformerenemy ai2d collision

My trigger dont work (2D platformer)

I'm beginner to unity. I want to make 2D platformer and i created first moving enemy. I wrote script which I think is fine but I wanted to make enemy go back and forth.

  [SerializeField] private Transform LeftCheck;
     [SerializeField] private Transform RightCheck;

So I've created 2 empty object and gave it box colliders and put in into the object and I wanted my enemy to flip every time he touches one of them so I wrote something like this. void OnTriggerEnter2D(Collider2D coll) { if (coll.gameObject == RightCheck || coll.gameObject == LeftCheck) { Flip(); } } Can you tell what I'm doing wrong? Both box colliders have isTrigger checked. I know the problem is with if condition cause eariler I did this using coll.gameObject.tag and it was working but I thought this way would be much more elegant. Sorry for my English too.

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
1
Best Answer

Answer by NSMcInroy · Feb 17, 2021 at 08:07 PM

Currently your code compares coll.gameObject (which is a type of GameObject) with RightCheck and LeftCheck (which are both of type Transform). They will never be equal.


Solution 1: - Grab the transform from the collider game object like this coll.gameObject.transform

 void OnTriggerEnter2D(Collider2D coll) { if (coll.gameObject.transform == RightCheck || coll.gameObject.transform == LeftCheck) { Flip(); } }


Solution 2: - Grab the gameObject from the two transforms like so RightCheck/LeftCheck.gameObject

 void OnTriggerEnter2D(Collider2D coll) { if (coll.gameObject == RightCheck.gameObject || coll.gameObject == LeftCheck.gameObject) { Flip(); } }


Solution 3: - Make RightCheck and LeftCheck GameObject types and skip the conversion entirely

   [SerializeField] private GameObject LeftCheck;
   [SerializeField] private GameObject RightCheck;

Solution 4: - As someone else has mentioned you may also want to check against tag as it tends to be more performant but seeing how this code doesn't seem to be called all that often it might be unnecessary. - In this scenario you do not need to keep a reference to the left and right checks and could just tag them accordingly.

 void OnTriggerEnter2D(Collider2D coll) { if (coll.gameObject.CompareTag("FlipCheck")) { Flip(); } }
Comment
Add comment · 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
1

Answer by GDGames0302 · Feb 17, 2021 at 06:41 PM

Use the variant you did earlier, coll.gameobject.tag . Never find or access objects by name, because it is slowing performance, always find objects by tag. Hope it helps.

Comment
Add comment · 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

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

161 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

Related Questions

Activate Trigger enable component doesn't work (2d mode.) 1 Answer

Tilemap Collider 2D not generating colliders...,Tilemap Colliders won't generate.... 0 Answers

OnTriggerEnter2D(Collider2D other) 2 Answers

Add audio. 1 Answer

Bombs as locomotion in 2D platformer 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