- Home /
How to set up items- triggers with rigidbodies
Okay, so I have some items that the player can pickup/drop. I need the items to be 'pickupable' just on triggerstay, and be dropped (and fall to the floor) on key press.
I have a box collider set to trigger on the item so that I can pick it up, but how do add a rigidbody and another collider so that it has gravity? I tried just that- adding another box and rigidbody, but then I can't pick it up anymore. I also tried to add an empty child object with a box collider intead so that I could have a rigidbody, but I still couldn't pick it up anymore. I thought it was because the new pysical collider was bigger than the trigger box, but even after making the pysics box smaller, no go.
How can I set this up?
Also, I need the physical box collider to be disabled when held- the player is blocked from moving properly when holding another box collider in their hand
thanks for any help!
Answer by superventure · Apr 17, 2011 at 05:33 PM
The moving trigger problem is very weird...and ANNOYING. Thanks guys for your help. I guess I am stubborn though, because I found a work around for a moving trigger:
If you want an object to both be a trigger and have pysics, You can have two colliders: the first collider must not be set to a trigger, then add your rigidbody, then add a second trigger(I used a sphere collider) and make it envelope the first collider, and set THAT to a trigger. For some reason, if you mess up that order of operations, the trigger will not be recognized anymore by the engine.
On a side note, the item when held was blocking movement to the player because of the active boxcollider. I tried to set it to a trigger just when equipped (and vise a versa when dropped), but again for some reason, it would only work one time only. SSOOOOO...
I dropped setting it to a trigger/not and added this bit of code
function Start() { ignoreCollision("Player"); }
function ignoreCollision(tag: String) {
var objects = GameObject.FindGameObjectsWithTag(tag);
for (o in objects) { if (o.GetComponent("Collider") && o != gameObject) Physics.IgnoreCollision(collider, o.collider);
} }
THIS IS THE ANSWER I WAS LOOKING FOR. Thank you holy crap. I thought I was going insane. Why doesn't the original collider work? Why is the order important? I am too new to this and I don't understand. Either way, thank you for posting this.
Answer by Statement · Apr 17, 2011 at 03:39 PM
There are problems with moving triggers not sending trigger messages, so my suggestion would be to use OnCollisionEnter instead. If you are stubborn still, try making a child object that has the trigger. But I doubt you'll get it working. It will work in some cases (such as something walking into the trigger) but not in other cases (such as the trigger "walking into" something).
Aw man, I was hoping that was not the case. Oh well. I just tried function OnCollisionStay(other:Collision){ thought, with the same results- am I setting that up right? Do I still have to have something set to trigger too?
I'd ditch the trigger entirely, if possible. However I find it a bit inconvenient too since sometimes you want to have an area of effect. In that case you could possibly do with Physics.OverlapSphere et.c.
Though I may be mistaken about the trigger not sending messages. I was using child triggers on character controllers for AoE behavior, but it didn't work. It only worked when someone walked into the trigger, not the other way around. It is possible (though I would be surprised if it was the case) that it would behave differently when parented to a rigidbody. Should be simple enough to set up a test on this.
Your answer
Follow this Question
Related Questions
Move player after colliding with trigger? 1 Answer
Playing sound through pickup trigger 3 Answers
Mission Objectives 1 Answer
How to make collectibles 2 Answers
Trigger problems... 1 Answer