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.
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 . I just like it.