Hello, I am having a problem with two objects colliding in the following situation:
Object 1 has a circle collider and moves in a vertical line, from top to bottom of the screen.
Object 2 has a box collider, standing still at the center of the screen.
Object 1 is positioned in a way that in the x-axis it’s close to Object 2, but the colliders never overlap. Object 1 is also the child of another object who moves horizontally to the left, on the FixedUpdate loop. In order to negate the horizontal movement, Object 1’s rigidbody has the same horizontal speed, to the right.
So what happens is that Object 1 moves vertically, but once it’s near Object 2, they collide. The transform position and the rigibody position never change in the x-axis, but for some reason the collision happens. If I remove the horizontal movements (from Object 1’s parent and Object 1’s rigidbody) the collision does not happen. I believe it is some sort of calculation in some part of the Physics engine, but so far I have no clue from the docs. Perhaps the collision checks use “both” positions of the object? Can anyone help?
With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.
This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.
Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.
I am moving the Objects using their rigidbodies. Once they are inserted into the scene the transform position is set and the rigidbody receives a velocity.
But while they move, I also have their parent object, which is sort of like a “layer” of elements that should be passing through the player. This parent object always moves on a constant speed to the left, with a transform.position += Vector3.left * speed * Time.fixedDeltatime; on the FixedUpdate loop.
That’s not moving with a Rigidbody2D, that’s stomping over the Transform. The whole point of the Rigidbody2D is to simulate physics and write that post to the Transform. Use the Rigidbody2D API to cause movement, never modify the Transform.
So, in order to achieve the effect of elements in the background colliding with each other and also moving constantly to the left as if in a conveyor belt, could I use rigidbody2d’s MovePositon()? If I get the current position of the transform, the current velocity of the rigidbody and add the constant speed that objects in this layer should be moving (conveyorBeltSpeed), would I still be preserving the calculations made during the physics update?