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?
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 :)
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?
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 ;
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!
Your answer
Follow this Question
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
Get() and Set() values of a gridArray from another scipt 0 Answers