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..?
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);
}
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.
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.
@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.
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.
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
Follow this Question
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