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 /
  • Help Room /
avatar image
0
Question by khubaib10 · Aug 29, 2016 at 07:00 PM · collider2dtriggersraycasthit2d

How to trigger functions like OnTriggerEnter2d and OnTriggerExit2d with raycasthit2d ?

Hello. I have been trying to trigger the OntriggerEnter2d and OntriggerExit2d function using RaycastHit2D. There are different sprites on which I have attached BoxCollider2d with Istrigger enabled. I want to trigger the above mentioned functions as RaycastHit2D hits the trigger. How do I trigger these function..?

Comment
Add comment · Show 2
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 Cherno · Aug 29, 2016 at 08:29 PM 0
Share

Why would you want to call these functions manually? Why not make both the trigger and the RayCast functions call a seperate function, so both can call the same one?

 void OnTriggerEnter2D(Collider other) {
      $$anonymous$$yFunction(other);
 }
 
 void OnTriggerExit2D(Collider other) {
      $$anonymous$$yFunction(other);
 }
 
 void Update() {
      RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);
      if (hit.collider != null) {
           $$anonymous$$yFunction(hit.collider);
      }
 }
 
 private void $$anonymous$$yFunction(Collider hitCollider) {
      Debug.Log(hitCollider.gameObject.name);
 }

avatar image khubaib10 · Aug 30, 2016 at 10:27 AM 0
Share

Cherno, I don't want to call these functions manually. I want these functions to trigger as soon as raycasthit2d hits the trigger more like interrupts.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Aug 29, 2016 at 08:45 PM

You can Search for "call outrigger from another function," but the answers aren't super clear.

In front of the OnTrigger, you have to add public. Then anyone can call it like a normal function: GO.GetComponent<otherScriptName>().OnTriggerEnter2D(cc);

Where GO points to the gameobject with the script, otherScriptName is the name of the script with the OnTrigger, and cc is your Collider2D (or whichever collider you want to say counts as hitting that trigger.)

So altogether you'd use:

 if(Physics.raycast2d(blah, blah, blah)) {
   hitData.gameObject.GetComponent ... (what I wrote above)

}

If you aren't comfortable with functions, or types (like how to get a Collider2D) and programming in general, this is going to be tricky until you are.

Comment
Add comment · Show 3 · 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 khubaib10 · Aug 30, 2016 at 10:30 AM 0
Share

@Owen-Reynolds, Thank you for giving me this information (I am new to unity) but the problem is still there... I want these functions to trigger as soon as raycasthit2d hits the trigger more like interrupts.

avatar image Owen-Reynolds · Aug 30, 2016 at 01:36 PM 0
Share

Raycasts never automatically do anything. They're just regular code lines and all they ever do is create data. If you want them to do something, you have to write the lines to do it, in your script, like in the example above.

That's pretty much how progra$$anonymous$$g (or making games in general) works. We write lines to check for A and then do B. And non-programmers think A causing B is automatic, because we made it feel that way.

avatar image khubaib10 Owen-Reynolds · Aug 30, 2016 at 01:58 PM 0
Share

Thank you so much. $$anonymous$$y problem is solved now. I used another technique to resolve my issue. You helped me indeed. Thanks.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Physics2D Raycast not colliding with Tilemap Collider 1 Answer

Problems using RaycastHit2D.collider, detection only on the rays origin 1 Answer

How to guarantee that only one trigger is activated at the same time? 0 Answers

OnTriggerEnter2D executed once 0 Answers

How to Deactivate a Trigger collider on a Specific GameObject 2 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