Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Body with friction keeps sliding off from a moving platform

Discussion in 'Physics' started by lacypittman94, Oct 17, 2023.

  1. lacypittman94

    lacypittman94

    Joined:
    Oct 17, 2023
    Posts:
    4
    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)
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    757
    Are you moving the platform by changing its transform.position?.

    You should move it with MovePosition() like so:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MoveX : MonoBehaviour
    4. {
    5.  
    6. Rigidbody rb;
    7.  
    8.     void Start ()
    9.     {
    10.         rb=GetComponent<Rigidbody>();
    11.     }
    12.  
    13.     void FixedUpdate()
    14.     {
    15.         rb.MovePosition(transform.position+Vector3.right*Time.deltaTime);
    16.     }
    17. }
    18.  
     
    Last edited: Oct 18, 2023
  3. lacypittman94

    lacypittman94

    Joined:
    Oct 17, 2023
    Posts:
    4
    This is the code that is moving the platform.

    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         Vector3 pos = transform.position;
    4.         transform.position += Vector3.left * 0.1f * Time.fixedDeltaTime;
    5.         rb.MovePosition(pos);
    6.     }
    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.
     
  4. lacypittman94

    lacypittman94

    Joined:
    Oct 17, 2023
    Posts:
    4
    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?
     
  5. lacypittman94

    lacypittman94

    Joined:
    Oct 17, 2023
    Posts:
    4
    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.
     
    zulo3d likes this.