Unity: 2D character floating using BoxCollider2D - unity3d

I'm pretty new to unity and I set up a simple scene with my character and the ground. I added a Rigidbody2D and a BoxCollider2D to the character and another BoxCollider2D to the ground. The boundaries of the Colliders fit the boundaries of the character / ground exactly, but my character is not falling on to the ground, when I hit play, but rather stops and floats a few pixels above it.
I've seen a lot of similar questions but the solutions didn't work for me (or maybe I didn't understand them right, as I mentioned above I'm new to Unity and thats my first game).
That's what the floating looks like:
I could obviously "fix" this by lowering the upper boundary below the actual boundary of the ground, like this:
But that's obviously not a good solution and I'd still like to know what is causing this bug and how to fix it.

Maybe gap has size of Default Contact Offset:
Set a proximity distance value for colliders to be considered in contact, even they are not actually in contact. Colliders whose distance is less than the sum of their contactOffset values generate contacts. This allows the collision detection system to predictively enforce the contact constraint even when the objects are slightly separated.
Caution: Reducing this value too far could cripple Unity’s ability to calculate continuous polygon collisions. Conversely, increasing the value too much could create artifacts for vertex collision.
You can find it in Edit > Project Settings > Physics2d .

Related

Unity character flies easily

I have been using Unity for a while and still Im not sure about some things, I know there are like 5 different ways to move our character (not using prefabs like for 1st or 3rd person controller I mean just code from scratch) so far for moving a character on a terrain I think setting the speed of the rigidbody works overal for me, just setting the x and leaving the y component as what it was before assigning that, so that gravity effect is kept, overal it works, I manage to collide with wall and other objects, manage to go on terrain and could add jump later in same fashion, but the trouble I find is that my character flies when it steps over any small object like a stone on ground, if he goes over it he starts flying, not a big distance but definetly looks like walking on the moon maybe, also I see that if I have a bridge that is lower in the middle part of it (like U shape) well the character goes like flying from the begining almost until the middle or so, as said works like being on the moon, however gravity seems ok on other objects, I know I can change the gravity value to be higher but Im affraid I could be breaking all that so my question is, is there a better way to move a character on ground that will work better without changing the gravity value? I guess that it can work right without affecting this value, I used other method to change velocity and adding a certain value to the y component but again I see it not right as an apparent correct value for small stones and small objects makes it hard to go over slopes too, any help is greatly welcome =).

Unreal Engine 4 - Collider component sweep issue

I have a 2D environment where an object falls (a trap) and a box collider component is moved with a timeline to follow the movement with SetRelativeLocation.
Since the trap is near a wall if I leave the Sweep option unchecked in some cases the character will stuck mid air between the trap and the wall.
Sweep off
If I turn on Sweep this will not happen and the character is pushed to ground correctly but since the collider "slows down" for some frames it seems to be in the middle of the sprite.
Sweep on
Teleport option seems to have no effect on this behaviour.
Is there any way to have the sweep effect with the collider keeping his velocity and keeping the structure as it is with the collider moved by a timeline?
I feel like I'm missing something really simple but I'm losing my head on this.
Thanks!
Since it's 2D, have you tried blocking the input when your character collides? I would add a simple collider in front of your character and when it is overlapped, I'd limit the input in the direction your character goes, ie.:
Character collides right
MoveRight(float Scale): Clamp Scale to (-1.f, 0.f); AddMovementInput(...
Hope it helps!

Unity 3D object with Rigidbody sliding

I have a cube with Rigidbody attached to it would slide slightly whenever I pressed the play button. If I leave it for awhile, it would slide to other side of the screen.
Anyone know how to solve this problem without using the "freeze position"? I don't know what I messed up in my project...
This is what the object looks like:
Note: I need to use the gravity.
Thank you!
Heh! The solution here is:
You had a rigidbody on the floor :)
You don't do that :) Never.
If the "floor" surface is flat, then,
it will not slide.
You've got something strange going on, such as
"floor" is NOT flat
a feature like "Wind" turned on
perhaps other objects invisible in the scene you have forgotten about are nudging it
PhysX does not have a "mind of it's own". There is some simple reason it is moving.
Let's say the "floor" is indeed on an angle, so it SHOULD move, but you WANT it to NOT move.
What you obviously do:
Just as in the real world, put something there to stop it moving.
A small invisible wall will do the trick. That's PhysX!
Usually rigidbody sliding happens when a lower rigidbody has lower mass than a higher rigidbody (forcing down the lower rigidbody). Typical problem with player having say mass 80 jumping on a cube with mass 1. In this case the collision is so violent that the cube will probably fly out (not only slide).
The situation is very similar to the real world. Try to stay on a box of milk if you have 120 kg (ok, ok, 80 :) ).
When you try to eliminate this behavior, you need either increase the mass of the lower object or decrease the mass of the higher one or set the lower rigidbody to kinematic.
The solutions above is not proper way of solving the problem. Unity has more features of physics than mass. If you get sliding on movement or because of other objects you should add proper drag value on your rigidbody. For example , lets say you have blocks spawning over top of other blocks and this creates horizontal sliding. In my case I add drag of 1 to the objects which has mass of ~ 1 kg. It depends on the scene and you should try different values on your case. Do not use bigger values and angular drag if it is not important.

Unity 5.2 - Moving character bumps between 2D edge colliders

I'm creating a fast-paced, 2D side-scrolling game on Unity 5.2, building my terrain out of discrete "blocks", each with its own EdgeCollider2D component.
Having a problem where my character gets bumped upward as it crosses from one block to another (imagine driving your car over a speed bump on the road).
This doesn't happen all the time. Seems to be random, which is even more irritating, as it makes finding a solution more difficult.
I've tried all of the suggestions that I could find for similar questions on this site, including:
Using CircleCollider2D's on the character
making sure the terrain blocks and their corresponding colliders are perfectly aligned. The attached screenshot shows one of the intersections.
changing the "Min penetration for penalty" setting to the minimum allowed value (0.0001)
switching between discrete and continuous collision detection for the character's RigidBody2D
increasing the mass and gravity scale for the character's RigidBody2D
... to no avail.
Beyond building a single, massive terrain object with a single edge collider from start to finish (which I'm trying to avoid), I've run out of ideas. Anything else I'm missing? Is it just a Unity bug?
Help!
Try detect the collision and set the vertical velocity to zero.
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name.StartsWith("block"))
rigidbody2d.velocity = new Vector2(rigidbody2d.velocity.x, 0);
}

Unity 4 Character Mesh Collider not aligned - character falls through. Image attached inc source unity asset link

Hello Peeps (first post yay).
I have just started to learn about unity and still going through various tutorials and learning the interface along with C#.
I am running Unity 4 and downloaded a free asset from the Unity store:
https://www.assetstore.unity3d.com/#/content/3174
The Problem.
I created a Plane in Unity.
Went to the Character and created colliders and imported it as a humanoid.
Then added a rigid body = The character fell through the plane
I then added a Mesh collider and attached the mesh for the character to the collider. The mesh does NOT align correctly to the character = the character fell through the plane.
I then tried box and capsule colliders and they worked fine. Attached these and the character would not fall through.
I then tried a character controller = Character did not fall through.
I want to have an accurate mesh for this test character so when an enemy hits him, there is an actual hit.
I have tried several other free characters from the store and they all have the same problem. Can someone please tell me how I can fix this? It seems I just either doing something completely wrong, missing a step or just plain lost.
Link below to image of the issue taken:
Using a mesh collider on a skinned, animated character is a wrong approach. Mesh colliders are generally used for environment geometry and rigid objects. This is all due to the amount of work it is needed for the Unity engine to accurately calculate the shape of the mesh collider, based on the provided mesh. More triangles means more work, and for the reasonably complex low poly character mesh of 10k to 30k triangles it is clear that the fps would most definitely drop to an unplayable level.
For our game we are using all primitive colliders, distributed on the adequate bone game objects. So, head bone has a sphere collider, spine has a box collider (for lungs), forearm has a capsule collider etc. That way,with a little bit of scripting, you can detect a hit and know exactly what part of body is damaged.
If your collider doesn't exactly match your mesh, then it's likely that you have the mesh applied to the wrong part of the object.
Try applying the Mesh Collider to the mesh, instead of the complete object.
You have a pretty complex character mesh, so using mesh collider probably isn't the best way to go. In many cases, character controller or capsule collider works fine; otherwise, you can just use compound collider and create a box or a sphere around various body parts of the character.
This once happened to me. The mistake i made was, i applied the mesh collider after i did some rotation and scaling. So i added the mesh collider as soon as i imported the model. And then it should be alright when you scale/edit the model. Also check with right/left handed co-ordinate system with unity and 3D MAX/Maya.