- Home /
For Some reason, collider check has extra collider.
I am making a small prototype of a Unity 2D game. I have 2 colliders for my character. This helps me because I want my character to be able to wall jump. This means that when I collide with the wall, my collider detection script assumes that I am hitting 2 colliders, and helps me customize animations.
//on the ground, change y position as necessary, and if you are pressing up, held = true
if (InAirDetection.AirDetect == 1)
{
position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
if (Input.GetButton("Vertical") == true)
{
held = true;
}
position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
}
//on the wall, change y position as neccessary, and if you are pressing up, held = true
if (InAirDetection.AirDetect == 2)
{
position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
if (Input.GetButton("Vertical") == true)
{
held = true;
}
}
//in the air, if held is true, change y. If you aren't pressing up, held is false.
if (InAirDetection.AirDetect == 0)
{
if (held == true)
{
position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
}
if (Input.GetButton("Vertical") == false)
{
held = false;
}
}
}
// apply the transformations
transform.position = position;
This is some of my code. I tried to make it so that if I let go of up in the air and pressed it again and nothing would happen. It works but there is a slight problem...
By slamming one collider(head) against the different collider(ceiling), the head goes into the body collider. This makes the collider detection thinks there is always one collider touching and that I am holding up. That means that I can jump about 5 times the intended height. Another side effect is that sometimes there seems to be a force acting on one the side of the object, like wind. This doesn't always happen.
How can I remove the bug completely? Is my only option to make the character have 1 collider?
I had an extra line of code. Also, I changed the collider's position a tad bit. This was the extra line:
position.y += $$anonymous$$oveUnitsPerSecond * verticalInput * Time.deltaTime;
Now, however, if I hold it and jump(or jump, let go, and hit up again before hitting the ground), I jump an extra 1.8/1.9 units higher.
Answer by lgarczyn · Dec 22, 2019 at 07:23 PM
Dude, you are using Update to work with rigidbodies (bad) while also affecting their position directly (bad) using transform.position instead of rigidbody.position(worse) and you have two colliders that are not supposed to interact on a layer with self-interaction enabled (also bad)
Of course you're going to get bugs!
Repeat after me
Transform.position for moving a physics-less objects
Rigidbody.position for teleportating a rigidbody
Rigidbody.MovePosition for moving a kinematic rigidbody
Rigidbody.velocity or Rigidbody.AddForce for moving a dynamic rigidbody
Yo, I don't know what that means. I'm a total beginner. I just used what I learned. I don't use the rigidbody at all in my code. Am I teleporting or moving the object? I don't know. All I know is that the bug is fixed and it was an error in my collision detection code. The only weird thing going on now is that when I hold the jump button, jump once, and then jump again without letting go, I go a bit higher.
If you're getting collisions, you're using rigidbodies. Please read about them.
I know that. Can you explain why the things you mentioned above are bad? I'm still using code similar to that, and everything is working fine.
Let me try to expand on his answer a little bit.
When you are using the physics system to move your character (using rigidbodies or characterControllers) you should use the FixedUpdate()
method ins$$anonymous$$d of the update method to do your calculations, because Unity's physics system ticks along at a different rate (potentially) than your actual frame rate. Putting your logic in FixedUpdate will mean that it is always called in alignment with Unity's physics updates. $$anonymous$$ake sure that when you use the FixedUpdate() you always use Time.fixedDeltaTime ins$$anonymous$$d of Time.deltaTime where applicable.
The purpose of using a rigidbody is that you want your character to interact with the physical world. You want it to be in charge of your object's position, so writing directly to transform.position will not only ignore whatever the rigidbody is trying to do but will also act funky with the physics system, which does not expect you to move the object without its permission.
Ins$$anonymous$$d, if you want to teleport an object, use Rigidbody.position. It will do the same thing, but will actually communicate with the physics system and will therefor play nice with it
If you want smooth movement (or movement that is stopped by things like walls) use Rigidbody.$$anonymous$$ovePosition.
If you have an object that is affected by forces (kinematic = false) then use Rigidbody.Addforce or Rigidbody.Addvelocity, because you want your movements to be purely physical as opposed to a kinematic rigidbody, which has non physical movement but interacts with the physics world
Answer by unity_ek98vnTRplGj8Q · Jan 03, 2020 at 09:16 PM
To more directly answer your question, if you want to put multiple colliders on an object but you don't want them to collide with eachother, you can put them in a Layer together, then in the project settings under "Physics" you can disable the layer from colliding with itself in the collision matrix