- Home /
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.
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.
@Fattie. Sorry, but despite you help me a lot, I don't think your answer is actually really answering my question. In fact you propose some workarounds, but your answer doesn't explain exactly why this happen, and which is a right and documented approach to solve my problem. I don't want to be critic. You have been very helpful and nice indeed, but I don't think that's enough to mark it as the right answer.
@Fattie: why did you remove the answer? Despite it was not exactly answering the problem, it was a lot helpful (in fact I upvoted it). I hope to not have offended you, with my comment. It wans't my intention.
@Dan$$anonymous$$arionette: well, I don't have that scene anymore. When I have time I could share it, yes. But that's quite easy to reproduce. Create a cube a plane, and attach a script with the described update function to the cube.
Answer by DanMarionette · Sep 25, 2012 at 09:56 AM
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.
First of all thanks to the answer. It's almost like @Fattie's one. I'm not a guru in Physics, nor in Unity. I'm just playing around with it these days and I'd like to understand some details I think importants to begin doing something more complex. "Why always the right?" : this is what mostly have scared me. It seems more like a bug or a bad use of applyforce method, rather than an expected behavior. It seems also that replacing the plane with a terrain, at least in some material configurations, solve this problem.