I'm trying to make a digging mechanism in my game by setting heights of the terrainData.
Everything works fine, but I found a bug. As can be seen on attached image, I put a Collider as a Trigger to check whether the whole terrain from the Trigger area is removed. I check it in OnTriggerExit method.
It works, but when I left some terrain 'in the middle' (as image shows) and removed terrain only around the Trigger is says that there is no collision with Terrain Collider (but it should be!!)
Thanks!
if your terrain has rigidbody in it will not detect collision because continous collision detection in terrain collider is not supported. one way to fix this is change terrain collider with box collider or remove rigidbody in terrain object.
Related
https://docs.unity3d.com/2021.1/Documentation/Manual/CollidersOverview.html
While getting some basic collision detection set up I referenced the official docs on the matter. Unfortunately, this left me more confused than when I started.
I made a simple scene. In this scene I added a sphere with a sphere collider (non-trigger) and a Kinematic Rigidbody. A script moves this object on the X axis when played.
I then created cube with a box collider (non-trigger). This object has no rigidbody or scripts attached.
Looking at the chart in the documentation linked to above, this should fall under the Static Collider and Kinematic Rigidbody Collider interaction, which should not create collision events.
However, if I add a OnCollisionEnter() to the sphere, it will call this method when the objects collide. Without physics they both continue on their way, otherwise oblivious to each other.
My concern then is that either the documentation isn't entirely correct or I have a fundamental misunderstanding of what it is telling me.
What's going on here?
I decided to make a 2D game for the PC on Unity, where the character moves around the stage. But I have a problem with the correct use of Rigid Body 2D.
In this picture you can see my playing field.
https://i.stack.imgur.com/PBhwx.png
I want my character to move around the entire field and when colliding with objects does not go through them.
My field is Tilemap, but I hung the Box Collider on the Grid. Boxing collider surrounds the grid.
But when I use Rigid Body and turn on the game, the object falls through. And if you remove the Rigid Body, then it just goes through objects.
I tried to use different types of Rigid Body, when using kinematic and static, sprite goes through houses.
The way it works is when you attach a RigidBody2D to an object, you give it physics properties, but it won't do anything useful without a collider. If you have an object with collider attached - every other object with collider attached will collide with it.
In your case the player should have RigidBody2D and BoxCollider2D. The ground should have BoxCollider2D, and basically everything you want the player to collide with should have BoxCollider2D. So the houses should have BoxCollider2D as well.
This setup should allow the player to walk on the ground without falling through it, and collider with houses.
Make sure that the collider's area is correct. You can preview it in the editor and edit it if it's not correct.
Also if you want it to work make sure the camera points down and your game setup is alongside Y axis so if there's a rigid body object falling down it falls down on the ground not before/behind it.
Very simple, I created an object that simply serves as a box collider that sits over the background. When the player character runs into it, I want the character to stop, but they just walk right through it. I have both of their colliders set to 2D box colliders, neither of them have the trigger option checked, and both are rigid bodies.
My guess is that you are using Transform.position to move the player.
All rigid body objects should be moved using the rigid body specific rigidbody.velocity function. Transform.position is practically teleportation while, rigidbody.velocity actually moves the rigidbody component. Allowing for the 2d colliders to take action.
Hope this helps!
I'm using a terrain in Unity 2019.2.1f1 and a custom mesh of a cave with a mesh collider.
My character that has a Capsule Collider way bigger than the entry of the cave should not be able to enter in. But due to the roundness of both colliders, he can come into force in the cave, glitching through the terrain collider.
I think it's velocity is not excessive, I'm moving the character with rb.MovePosition() in the FixedUpdate(), and I set its rigidbody collision detection to Continuous speculative (tried all the "continuous" modes)
In the animation below, you can see the mesh of the cave and the capsule collider around the character.
How can I prevent this from happening? How can I say to Unity: "I want the colliders to be rock solid and not marshmallow"?
Colliders in Unity are rock-solid. They are incapable of any kind of soft physics, and the only moment they will warp through each other is when you force them to.
Here, you are setting the rigidBody position by force, into an impossible location. The game tries its best to fit your rigidBody despite the lack of room.
You can
Only use forces, and velocities. You can simply set the velocity to the direction of your choosing, and set it to 0 when you stop moving, or use AddForce, which is basically the same thing.
Keep using MovePosition, but use a SphereCast or CapsuleCast to check if you have enough room to move first.
If a rigidbody is below a terrain it is getting dragged up on the terrain
I have 2 terrains, one over another to make a cave like shape. If I put a rigidbody on terrain below it is getting dragged onto the terrain above. So i cant even put a single rigidbody inside that cave.
I tried with mesh collider on terrain obove instead of using terrain collider. But terrain does not have an actual mesh, so it didnt work.
Is there a solution to overcome this?
Since Unity's TerrainCollider is height-map based, you can't put one on top of the other and expect proper collision detection.