Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Dragonrider-Liu · Feb 11, 2018 at 01:02 PM · scripting problemscripting beginnercollider2dscriptingbasicsevent triggering

Is it possible to call OnTrigerCollider2D() without binding any script to the Collider2D directly?

I want to use a single script to control all elements of one scene. Let's call it "Director" script, bound to EventSystem.

I knew that I can get a GameObject by name or tag, and then manipulated the components such as transform. Thus I wondered if I can define the OnTrigerCollider2D() in this "Director” script without binding any script to the Collider2D GameObject, just like I define the value of the transform? If possible, how?

According to some friend, it is necessary to bind at least a basic script to the Collider2D to authorize the "Director" script to get access to the Collider2D event, no way to bypass this limitation?

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
0

Answer by Cuttlas-U · Feb 11, 2018 at 02:56 PM

hi; I like that :) I also always want to do every thing on 1 script ; thats some kind of give me better control over every thing;

u cant call onTrigger enter without attaching it a script but there are other ways;

as i saw before in a unity tutorial what u can do is "Physics.SphereCast"

it creates a sphere around that object that u want but u can do this in your "Director" Script too;

u can have a list of objects and send the order to do the cast then return if they have imapt with another object or not;

i think but im not sure that "Physics.SphereCast" is a little heavy so what u need to do is create a time counter in your update method and do this cast after some frames passed like this :

 void Update()
 {
 float TimeNeedToCast = 1;
 float CurrentTimePassed;
 
 if ( TimeNeedToCas < CurrentTimePassed)
 {
 // do all of your objects casting here
 CurrentTimePassed = 0;
 }
 else
 {
 CurrentTimePassed += Time.detlaTime
 }
 
 }

so this will cast only after 1 sec ( not each fram ) so u are not worry about the heavy problem any more :)

Comment
Add comment · Show 4 · 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 Dragonrider-Liu · Feb 11, 2018 at 03:11 PM 0
Share

It looks cool. I will find the related APIs, and try it in the project.

And one problem may be how heavy it is, for example compared with a simple Vector3.Distance() to check the distance of two different objects?

avatar image Cuttlas-U Dragonrider-Liu · Feb 11, 2018 at 03:19 PM 0
Share

i think the distance is more heavy;

first of all sphere cast can give u all the objects that are inside the sphere ;

so u dont need to check like all the enemies to the player;

u just cast a sphere and know what objects are inside ;

but with distance ( wich is a heavy api ) u need to call it for all the enemies cause u dont know witch one of them maybe in range so u need to check them all ;

avatar image Dragonrider-Liu Cuttlas-U · Feb 12, 2018 at 08:19 AM 0
Share

Thanks! You are right, another person also suggest a collision manager monitoring all collisions theoretically should do the job, but just like you said, like the distance method, it cost too much, since there maybe hundreds small collider2Ds in one scene.

Then I need to find answers for two questions:

Is the resources cost acceptable and How to manipulate the shape of the cast?

Thanks for your solution again!

Show more comments

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

193 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

Related Questions

How to disable raycast/groundchecker on OnTriggerEnter? 0 Answers

Set public gameobject by raycast hit target 1 Answer

my script was written but does not want to work 1 Answer

Displaying a different sprite depending on which angle a character is viewed from 1 Answer

When I change a bool in a script on one object it doesn't change the bool on the same script on another object? 1 Answer


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