I have a platform with a rigidbody and a box collider. The IsKinematic box is checked. And I have a box over the platform, again, with a rigidbody and a box collider. When I click start, the box falls on the platform and stops, as expected. But the platform itself is moving towards left slowly. The problem is the box does not move with it(the box sits still) and slides off after a while. I tried increasing drag values of both objects. Did not work. I added physics material to both objects(with non-zero friction values). Did not work. I am a beginner and I don’t know where the problem is. How to fix the issue? (I did not attach any image since the scene is simple enough to visualize)
Are you moving the platform by changing its transform.position?.
You should move it with MovePosition() like so:
using UnityEngine;
public class MoveX : MonoBehaviour
{
Rigidbody rb;
void Start ()
{
rb=GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.MovePosition(transform.position+Vector3.right*Time.deltaTime);
}
}
This is the code that is moving the platform.
void FixedUpdate()
{
Vector3 pos = transform.position;
transform.position += Vector3.left * 0.1f * Time.fixedDeltaTime;
rb.MovePosition(pos);
}
And when I hit play, I see the platform move. But the box that is sitting above does not move with it at all.
Platform RigidBody: Mass: 1, Drag: 0.93, Ang.Drag: 1.14, IsKinematic:YES
CubeRigidBody: Mass: 1, Drag: 1.14, Ang.Drag: 1.05, IsKinematic:NO
There’s also a physics material with dynamic and static friction values of 0.6 attached to the BoxColliders of both objects but I’m still having no luck.
Now that I think about it, the platform should not move at all. It should teleport with transform.position and move back to its original position. That means MovePosition(pos) is not working. Then the platform moves only with updating the transform.position, which explains why there’s no friction between the objects. But why does MovePosition not work. Does that have to do with Update vs FixedUpdate?
It’s solved. The problem was that I was using both transform.position and MovePosition in the same block. It nullified the effect of MovePosition and therefore there was no friction effect on the cube.