How to prevent sliding with rigid bodies in Unity? - unity3d

So, I am using Unity's Rigid body physics engine. And I made the player using a capsule collider. And after creating another rigid body with a Box collider I tried to jump onto it, only, the player starts to ever so slightly slide on top of it until eventually sliding off.
I want to be able to make the player jump onto movable objects without sliding on top unless there is enough of a slope to make sense for the player to slide off of. Is this possible, or does Unity's physics engine prevent this?

This can be achieved by applying a Physic Material to the player's capsule collider. Specifically, take a look at the Dynamic Friction and the Static Friction of the Physic Material. You may also want to set the Friction Combine to Maximum, if you want to ensure your player has a good grip on the platform regardless of what kind they land on.
A higher Dynamic Friction will quickly stop your player object if it starts sliding, while a higher Static Friction will make it harder for the player object to start sliding if it is otherwise stationary.
There isn't an exact science behind choosing these values; you'll really have to play around with it to see what feels right for your game.

Related

It is impossible to implement accurate collisions in 3D SpriteRenderers? Is Billboard incompatible with rigidbodies?

I've been having difficulty implementing enemies with a billboarding system for a while:
For a new project I'm reusing some animated sprites from an old static camera shooter game I made. It consisted of shooting projectiles and hitting enemies advancing towards your position. The enemies were capsules which had an animated plane in front of them.
I wanted to go a step further and make a raycast system that would detect if the impact on a plane is on the alpha part of its texture. I achieved it using a SpriteRenderer and a plane that matched the size of the sprite which receives the impact of the Raycast.
The problem comes with the movement of the enemies. If I use Rigidbodies this conflicts with the plane which detects the impact, since I have to use a non-convex Mesh Collider for RaycastHit.textureCoord (With Convex colliders it doesn't return the correct position). I need Rigidbody based movement for the enemies to push each other when colliding.
I tried using Transform Movement, since the error does not occur, but this allows enemies to pass through each other and occupy the same space.
I started investigating navMesh but I'm not sure if it is possible to avoid collisions.
I started to place a group of trigger colliders on the enemy mimicking the shape of the sprite. But this makes it difficult for me to detect collisions when enemies do certain animations (Some enemies jump diagonally or their animations are a bit jittery).
I would like to keep the impact detection system for the sprite, but maybe there is another way to check the texture at the impact location.
I think my options are:
With rigidbody based movement: separate the animation and hit detection system from the enemy movement. And make the billboarding effect teleport to the object constantly.
With Transform movement: Detect if they are overlapping and move them in opposite direction to the overlapping to simulate the collision.
With motion based navMesh: I'm not sure if it will be useful to me, since the characters jump and I also believe that a Mesh Agent ship cannot simultaneously be an obstacle. I understand that the navMesh cannot be generated in runtime.
Leaving texture based detection and using the trigger colliders group: Animate the colliders to also follow the animations. Or simplify everything to a large collider, losing precision on impact.
-Find an alternative method to detect the impact coordinates in the texture, avoiding using RaycastHit.textureCoord and therefore Mesh Collider. (Although I would like to keep the impact detection system as it is, maybe there is a similar approach for the same result).
The reason I have not yet delved into these solutions is because I would like to keep the system simple. I'm trying to avoid separating the enemy into 2 gameobjects, simulating collisions or animating collider positions. I honestly don't know which way to go in order to move forward with the project.

Unity - How to give some flexibility to colliders

Hi I'am new to Unity and I was trying to implement a game using tetris blocks.
The game's goal is to build the highest tower before it collapses. However there is a problem in my implementation which is seen in the picture below.
I achieve the building a tower task by activating the rigidbody gravityscale when it collides with something. With that way it can collapse after touching somewhere not before. But I want to have the flexibility of some collisions. In the situation seen in the picture below, that 'T' block will collide with the point in the red circle before landing safely and gravityscale of the rigidbody will be set. So it will drop but I don't want it to happen becasue the collision area is too small. I want to make it land safely with some flexibility.
I tried to make colliders' size 0.9 but that just disrupts the scale of the world.
Can I do something like this :
If collision happens, check collision area and if the area is lower than lets say 0.1, do not trigger rigidbody gravityscale.
what about using capsulle collider 2D with small radius?

Climb down ladder in 2D platformer game

For my game, the player character requires to climb up and down ladders those are placed in the gameplay area.
At present, I can able to climb up for my player character to climb down at present I don't have anyway. Because platform box collider applied with platform effector, so for the climb up, effector does not create any problem but now after reaching the top, it becomes solid platform so now I can't able to move downside.
For climbing up, I have followed this tutorial: How To Make 2D Ladders In Unity - Easy Tutorial
I am looking to implement some physics so I can reach downside to the ladder after reaching top.
You need 2 boolean variables isClimbingUp and isClimbingDown which depend on pressed key and a second ray, which will check -Vector2.up direction. Then just add one more 'else if' statement for down direction.
Yes, I have managed to solve this, and the game is published in the stores.
You can check using the below link:
Humpty Trumpty's Border Wall
This is my overall physics setup for the stair:
I have applied a trigger collider to my stair object and disable player gravity scale when the player within the stair.
Then after within the trigger enter and exist, I have done this:
Physics2D.IgnoreCollision(m_CapsuleCollider, myStair.platformCollider, false);
Disable collision between player and platform colliders when the player is within the stair.
I don't think, Platform Effect 2D becomes useful to me in this process but I didn't remove this to remain in the safer side.
So you have to keep a reference of the platform object which is attached to Stair.
I hope you will get a general idea to solve this problem.

uNet rigidbody movement latency issue

I am making a Multiplayer FPS using this tutorial by Brackeys and am using a rigidbody for Player movement.
If i move forwards on computer 1, then there is about a half second delay until the player stops on computer 2.
I don't know if this is normal or if it stops when you start using paid servers.
Thanks.
Play around with linear drag and drag on your player's rigidbody and see what fits your needs. Usually 999 linear drag and 1-5 drag does the job for me. Also, you should change physics material on your player's collider according to current state - if he is moving set it to zero friction, and when he is not moving - switch to max friction.
For the player you should use a CharacterController instead.
For that very reason.
The traditional Doom-style first person controls are not physically
realistic. The character runs 90 miles per hour, comes to a halt
immediately and turns on a dime. Because it is so unrealistic, use of
Rigidbodies and physics to create this behavior is impractical and
will feel wrong. The solution is the specialized Character Controller.
It is simply a capsule shaped Collider which can be told to move in
some direction from a script. The Controller will then carry out the
movement but be constrained by collisions. It will slide along walls,
walk up stairs (if they are lower than the Step Offset) and walk on
slopes within the Slope Limit.
The Controller does not react to forces on its own and it does not
automatically push Rigidbodies away.

Make two physics objects not collide but do detect collisions in Unity

I have a Unity project in which there is a 2D game world that consists of static colliders to make the geometry solid to the characters that inhabit it. The player is a dynamic collider (with a non-kinematic rigidbody). There's also an enemy character that is also a dynamic collider. Both characters walk over the floor and bump into walls like I'd expect them to.
What I want to achieve is that the player and enemy are not solid to each other, so they can move through each other. I achieved this by putting the enemy and the player on separate layers and setting the collision matrix so that these layers do not collide with each other. The problem I'm having now, however, is that I do want to detect whether or not the enemy and the player ran into each other. I added a trigger collider to the enemy character, it's on the enemy layer which means it doesn't detect collisions with the player.
I thought of making a sub-gameobject for the enemy, put it on the player's layer, add a rigidbody and trigger collider to it and use that to detect collisions between the player and the enemy, but it feels so convoluted that it leaves me wondering if there isn't a more elegant solution for this.
Edit (2020-05-26): Nowadays you can tell the engine to ignore collisions between two given objects. Kudos to Deepscorn for the comment. Will leave the rest of the answer unchanged, and read with that in mind.
Yes, you need to create a child GameObject, with a trigger collider, and put it in a layer that interacts with the player layer.
No, you don't need to add a Rigidbody to the new GameObject, the parent's rigidbody already makes it a dynamic collider, so you will get OnTrigger events.
As a side note, just to keep things organized, if you create a child of the enemy don't put it in the player layer. For example, in the future you might need to disable the player's layer collision with itself. Furthermore, if your player interacts this way with many objects, I'd put a single trigger on the player instead of the enemies, on a separate PlayerTrigger layer, just to keep things simple.
Isn't there a simpler way? Not really. You definitely need non-interaction between the player and enemy colliders, but some kind of interaction between them too. So one of them needs to span two layers, or the whole interaction would be described by a single bool. The physics engine processes lots of information in one go, so you can set all the layers and collisions you want, but during the physics loop you have no further control on what happens. You can't tell the engine to ignore collisions between just two objects. Having only 32 layers, and having them behave in the rigid way they do are actually heavy optimizations. If you are concerned about performance for creating another layer, disable interaction between layers you don't need, like the trigger layer and the floor and walls, or layers that don't even touch.
Your alternative is doing it all by code, which is even less elegant. A single child capsule on the player doesn't sound that bad now, doesn't it?