Moving a rigid body on a plane applying forces

I have the following objects in my scene:

  • A plane with a BoxCollider
  • A cube mesh object on the plane
  • A BoxCollider attached to the cube object
  • A RigidBody attached to the cube
  • A script attached to the cube object

I’m trying to move the object along an axis applying a force to its center of mass only along the Z axis.
The code I have inside my script is basically the following:

public void FixedUpdate()
{
   if (PressedTheButtonToMoveTheObject())
   {
       this.rigidbody.AddForce(this.transform.forward * forceValue,ForceMode.Inpulse)
   }
}

The cube object, is correctly align with the axis, and the force impulse is applied strictly along the Z axis.
The objects moves forward but it shows a rotation around Y axis, like if a torque was applied to it.
I’m struggling with these issues from a few days, but I can’t understand which is my mistake.

Could anyone help me?

Shouldn’t the object keep moving strictly forward? If not why ? And how could I eventually change the code to make it move the object strictly forward?


EDIT:

here’s a screenshot showing my mesh rotated after a few seconds impulses are applied.
3490-debug_unity.jpeg.png

Don’t mind the spheres and the line. They are displayed through Gizmos.Draw function. The sphere in the middle is the center of mass. I used them to be sure that the rigid body was placed correctly in relation with the mesh position. It seems to be fine.

The material of both plane and cube is a custom material with this parameters:

  • Dynamic friction 0.5
  • Static friction 0.1
  • Bounciness 0
  • Both Combine are 0

I know this keyword isn’t necessary :slight_smile: . I just like it.

I have done some experimentation and from what I can see the cube is turning due to the effect of friction. If you turn friction off then it maintains a straight line. You can compensate for the lack of friction with some drag to get similar results. Even with drag though the cube does kick off centre a little but, almost unnoticeable.
Because you are adding force in the direction of the transforms forward, any slight change in direction caused by friction would just be multiplied. In real life, friction can cause this to happen quite easily.
Why always to the right? I’m not sure. I don’t pretend to know how Unity’s physics is implemented but, this is simulated friction after all. You can also get different results when you change the collision detection on the rigidbody.

I’m sorry if this was like the previous answer, I didn’t get to see it myself. Bottom line is though, I’m not sure this is something to be corrected without workarounds. You can always remove friction or better, lock the x position and y rotation on the rigidbody constraints.

You wouldn’t really want to move an object like this and expect it to go in a straight line in unity or real life.