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
2
Question by Lava_Key · Feb 04, 2020 at 07:15 PM · 2d platformer

C# code to check whether two objects are touching

My friend and I are both making a 2d game with c# ,but we can't figure out how to make an if statement to check whether two objects are touching so can anyone tell me the code needed to check if a player is touch the goal for example.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Daniil-Manokhin · Feb 04, 2020 at 07:21 PM

To check whether the player is touching a specific object:

  1. Go to your goal object

  2. In the inspector window go to tags

  3. Make a tag, for example "goal"

Add this code to your player object (make sure the player or goal has a RigidBody):

 void OnTriggerEnter(Collider other)
     {
         if (other.transform.tag == "Goal")
             // Do stuff
     }

Hope this helped!

Comment
Add comment · Show 5 · 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 Lava_Key · Feb 04, 2020 at 08:53 PM 0
Share

Thanks so much.

By any chance do you know how to make the player get launched up. (As if it was on a bounce pad)?

avatar image kbartek Lava_Key · Feb 04, 2020 at 10:03 PM 0
Share

If you have an attached Rigidbody, you can do something like: GetComponent<Rigidbody2D>().AddForce(transform.up, * <Launch strength>, Force2D$$anonymous$$ode.Impulse) (haven't tested, but it should work)

avatar image Lava_Key kbartek · Feb 04, 2020 at 10:10 PM 0
Share

It doesn't seem to work but thanks anyway.

avatar image Daniil-Manokhin Lava_Key · Feb 04, 2020 at 10:27 PM 0
Share

Sorry, I don’t have unity open at the moment to give you an answer which I can test but you can try the code here:

https://answers.unity.com/questions/1216376/help-2d-launch-an-object-in-a-particular-direction.html

Hope it works, let me know!

avatar image SevenMD · May 07, 2021 at 02:54 PM 0
Share

Thanks for this!

I thought I'd point out that my IDE actually gives me a heads up that comparing tags with "==" is inefficient, and that there's a builtin method that is much faster for doing just that.

  void OnTriggerEnter(Collider other)
      {
          if (other.transform.CompareTag("Goal"))
              // Do stuff
      }

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

122 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

Related Questions

2dToolkit and Physics2D.OverlapCircle 2 Answers

Inconsistent 2D collision detection since 5.2.3 1 Answer

Deactivate Key. 2 Answers

How do i make a player or move around any side of a platform 2 Answers

Unity 2D: Variable Jump Height with a Double Jump 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