- Home /
2D Collider/Trigger on Canvas
Hi there, I'm making a little simulator and I depend on a pressure gauge to activate/deactivate some elements on my scene, this gauge is shown in a UI canvas, so far I've been using some math within the update method but I want to see if I can enhance it by using some triggers on my gauge.
Problem is that I can't find a way for the needle to collide with the green bar, depending on the hierarchy arrange it simply goes over or under the bar but never collides, both of them are in the UI layer
How can I set this up? The needle has a 2d RigidBody 2D as kinematic with continuos detection and for the other colliders I have them as box colliders also tried with rigidbody 2d attached to the images and nothing seems to work, I did check the layer collision matrix and is set to UI/UI what else I'm I missing here, thanks a lot!!!
Answer by Diukrone · Jan 17, 2018 at 06:42 PM
The better way, to my way of to code, is to do checks on code, using SmoothDamp. So when pressure reach some value, it triggers an event on or off. So trigger do calls for animations on GUI element. I dont know to much about 2D Physics, so i cant help much ^^ desires to you good luck
Answer by FallingRocketGames · Jan 17, 2018 at 09:33 PM
So my workaround was to use 3D colliders instead and extend them over the Z axis in such way that the needle was unable to miss it, can't say if this was the best way to do this but it worked, yet I'm still curious as why the 2D collider didn't work in my UI.
Other question, do I need to make a script for each trigger? I made a script for each one of the triggers and attached it to it's respective game object like so:
private void OnTriggerEnter(Collider needle)
{
if (needle.gameObject.tag == "Needle")
{
print("Max Pressure Reached");
//Some more instructions related to the active trigger 9, 7, 5, 4, 0...
}
}
and so forth with the other ones :( how can I put them all in a single script?
I am happy that you got a solution! I am curious also that 2D colliders not worked. Yes, you can put many scripts inside, just use in FixedUpdate a call for calculations. For example, you can use the call OnTriggerEnter(); following the logic above:
void FixedUpdate()
{
method1();
method2();
}
void method1()
{
}
void method2()
{
}
Your answer
Follow this Question
Related Questions
UI - clamp the movement of a panel 0 Answers
Difficulties with world space canvas interaction 1 Answer
Display loaded UI Image in Container with native size 0 Answers
UI Image tries to 'fit' source image ignoring transparent pixels and stretches it 0 Answers
Converting to "Rect Transform" permanently deleted part of my project 0 Answers