- Home /
seeking collision scenario solution : falling anvil vs. player
I'm creating a 2d platform, and have a scene with a platform with a boxcollider2d, a player gameobject with a non-kinematic rigidbody and 2d colliders, and a gameobject that works like thwomp from mario, its kinematic and has a box collider and moves using velocity in fixedupdate. Is there anyway when the thwomp comes down it does not clip in to the player or ground. I used a trigger event to set velocity to zero but it still doesn't work. Also when I set the thwomp to non kinematic, it works but I'm able to push it around with the player, which I don't want, I can set the weight really high but then the player gets pushed through the platform, or it bounces off the ground. So any idea's of how to do this with out clipping.
I renamed your question to be more descriptive. What reaction do you want to achieve ins$$anonymous$$d? If neither clips into the other and neither can push the other, that's a paradoxical requirement.
You must have a plan for every conceivable outcome, so not just the thwomp vs. the player, but the thwomp vs. every other entity archetype. First thing that comes to $$anonymous$$d is that any entity in contact with the bottom edge of the thwomp while it's in motion should first be pushed until grounded, then die instantly. You could replace it with some kind of gibs or a smushed version of the entity. These gibs or smushed versions should not have collision dynamics with the thwomp, and should lie at a 2D depth "behind" the thwomp. What else did you have in $$anonymous$$d?
Well ideally this is what I want to happen. There are 2 scenarios I can think of for the thwomp 1) is a wall/ground/moving platform, in this case i just want the thwomp to collide with the ground and then wait a second or two then return to its neutral position, 2) player or enemy, which is usually a box collider and circle colliders for head and legs, in this scenario i want the thwomp to hold the player down for a second and apply damage then return to a neutral position. I can achieve this by making the thwomp a non-kinematic rigidbody, but then the player is able to push the thwomp, I want the thwomp to be able to push the player, not the other way around. Sorry for not being more clear earlier.
Okay, I have a better picture of the problem, I think. From your description, it does seem as though the thwomp object needs to be kinematic. A non-kinematic version might be quite interesting, and probably a lot easier to script for, but based on your stated requirements, it needs to be kinematic.
You can think of kinematic as being a toggle that says, "I will script all necessary responses to collision events for this object". Then it's your responsibility to actually do so, for every conceivable situation. When two thwomps meet, or one touches a moving platform, or a static platform, or a player, or any other object... It can get pretty messy if you're not careful.
You're also driving state logic based on these interactions. That's where the complexity can grow exponentially, depending on your requirements. For instance, it might respond one way to one scenario during its "attack" state, but respond another way to the same scenario during its "reset" state.
It's easiest to think of these Logic $$anonymous$$atrices as "who gets priority" in a hierarchical fashion. A thwomp-like object will probably supersede most others, it being a big bad unstoppable agent of destruction, and thus any other bodies interacting with it should give it the right-of-way.
If it collides with a player, the player will be pushed. It can detect that it's touching a player, and push the player until the player is mashed against a floor, wall, ceiling, etc. At that moment, the player might enter a special "stunned" state, denying the user permission to act. This stunned state might also be the thwomp's "rest" state, prior to returning home. In this specific circumstance, the player receives damage.
$$anonymous$$ixing pure physics, scripted kinematics, stateful behaviors, and other complex logic in a deter$$anonymous$$istic simulation is not an easy task. It requires exceptional care and planning, as well as a fairly comprehensive understanding of PhysX and Unity principles.
It may be worth considering alternatives that simplify your scenarios. Then again, if you have a strong enough command of the language and systems, you can create some highly modular and "foolproof" entities for your level designer to play with.
I don't think I'll be able to do it just kinematic then. I experimented more and made the thwomp nonkinematic and then attached a child object bounds, that has a kinematic rigidbody and box collider. This seems to work like a charm, but only problem is I have a trigger on the thwomp that detects if it collides with the player, only problems is the thwomp is moving too fast, so when it hits the player it pushes it through the platform, before stopping. Is there anything I can do in physicsmanager2d to make it detect collisions faster or anything else.
$$anonymous$$ind of. You can decrease the physics timestep for your project to have more physics calculations per second. This is rather expensive, and should be avoided if possible, unless the rest of your game requirements are light enough to support the expense. You can change a rigidbody's interpolation mode to change the way PhysX detects collisions with a particular body. See the rigidbody documentation to learn more about when to use each variant. Also this question seems to directly relate to this subject.