- Home /
Moving Object with a Rigidbody and Capsule Collider falls from the crack between two Box Colliders.
Hi, I'm currently working on a 2.5D Platform game where the environment is 3D but the game is played on 2D. I've come to work out my own physics system (except Collision) since Unity's own physics feels weird on platforming games (such as moving character with Rigidbody.AddForce). I'm designing levels like it is a tilemap, so my levels looks like this in general:
The problem is that my player object falls through the crack between these Box Colliders. The player object has Rigidbody and a Capsule Collider on it, and I'm moving my player via transform.position. I've made minor changes on Unity Learn's 2D Character Controller tutorial sample code. Here is the part of my code where I move my controller:
private void FixedUpdate()
{
if (ControlsOn)
{
// X Movement Calculation
movementVector = Vector3.zero;
movementInput = InputManager.Instance.Controls.LustControls.Move.ReadValue<float>();
if (movementInput > 0.5f) movementDirection.x = 1;
else if (movementInput < -0.5f) movementDirection.x = -1;
else movementDirection.x = 0;
velocity.x = movementDirection.x * (maxVelocityX / friction) * Time.deltaTime;
deltaPosition = velocity.x * Vector3.right;
movementVector = deltaPosition;
Move(movementVector);
// Rotation
if (movementDirection != directionMemory)
{
// TO DO -> Do rotation
}
// Y Movement Calculation
movementVector = Vector3.zero;
if (velocity.y > 0.0f) movementDirection.y = 1;
else if (velocity.y < 0.0f)
if (!isGrounded) movementDirection.y = -1;
else movementDirection.y = 0;
velocity.y += Physics.gravity.y * gravityModifier * Time.deltaTime;
velocity.y = Mathf.Clamp(velocity.y, minVelocityY, maxVelocityY);
deltaPosition = velocity.y * Vector3.up;
movementVector = deltaPosition * Time.deltaTime;
Move(movementVector);
directionMemory = movementDirection;
}
}
And here is the Move() function:
private void Move(Vector3 motion)
{
transform.position += motion;
}
I tried grouping meshes with ProBuilder tool, tried to give my level's ground a single Mesh Collider like a terrain but that also didn't work because of the concave meshes' surfaces are made of triangles (which makes the mesh have more cracks on it). I'm stuck on this for over a week now and thought I should give a shot on the community. So I'm open for any advice on the structure or the problem. Thanks! :)
Your answer
Follow this Question
Related Questions
Quickly moving cubes fall through quad floor 3 Answers
ways to get Collision2D 2 Answers
Compound collider object behaving as a whole? 1 Answer
Keep Horizontal Momentum after Jump 2 Answers