I am looking to detect collision on a floor button, but would like to avoid using triggers if I can.
The button would have to detect both the “Player” tag and “Box” tag, and my initial idea was to have it function similarly to the buttons from Portal, where the player or cube can stand on them and it triggers an event, but when that object is removed, the event ends (door closes for example).
I have done this before using triggers, but this requires quite precise positioning of the trigger and takes a little while to set up.
If it is enough for your game to check in 2d space u can use rect.
and than use the Contains method of Rect so you can check if a specific point in in this rect. if this is enough for your purpose.
Otherwise if you need a check if a 3d cube is 50% or 100% in another one you have to code it on your own. You can also use colliders already attaached to getting their bounds in x y z and than calculate everything by code
If you could post a screenshot of your “button” , is it a on the ground lying one or how should i imagine ?
How does using triggers require ‘precise positioning’?
I can guarantee you that buttons ala portal work just fine with triggers. Sure, the initial setup of the trigger might take a minute, and you might want to change it’s size ones or twice, but it would take a hell of a lot less time then writing some custom collision detection script.
Personally I would just use triggers for your described scenario.
I did some more research and discovered something called OnCollisionEnter, the only problem is that it doesn’t seem to work.
I have set everything up as it should be (as far as I can see), but my debug line never fires, and nothing is ever run.
Try moving your Debug.Log up a line in case it is just the if statement that is buggy. Learning the debugger would be very helpful in this situation.
Also, from the manual, make sure this doesn’t apply in your situation:
I have to add, I’m with Timelog on this; a trigger is more efficient. Maybe you could create a button prefab with the trigger area pre-positioned and use that wherever needed.
I have RigidBody’s on both objects, my testing cube and my button.
The button is Kenematic to prevent people pushing it round the floor, but the cube has full physics applied and falls onto the button when played.
After adding more debug lines, it appears that this line is the culprit, but I need to be able to check if it is 1 of 2 tags, have I somehow written this wrong or something?
if (other.gameObject.tag == "Box" || other.gameObject.tag == "Player")
The issue was the tag, for some reason it wasn’t accepting the tag on collision, I deleted the cube object and made another one with the same tag and it is now working.
the only issue remaining is the player collision doesn’t seem to be recognised.
The cube tagged Box now works fine, but the player, tagged Player will not run through the code, as if collision with the player is somehow not counted.