AddRelativeForce miscalculations

Good day kind people,

Let me start with explaing my intentions:

It is my intention to create 1 cube that can be moved around a level by pressing the WASD keys, this gave me no problem and you could easely move the cube around the level.

This however was done by using the Transform.position, but i wanted a cube that had physics, so i changed the Cube to have a Cube collision and a Rigidbody,

Now in stead of using the Transform.position (which wouldn't give me my physics) i used the AddRelativeForce (which would add force to the physics object forward into the direction it is rotated to, correct me if im wrong please)

Done so using the following code:

float v = Input.GetAxis ("Vertical");
float h = Input.GetAxis ("Horizontal");
Vector3 relForce = new Vector3(h, 0, v);

mybody.AddRelativeForce(relForce * 20);

Now here is my problem:

The AddRelativeFoce function isnt doing what i think it should be doing, the 2 things that i noticed:

1: It isnt acurate in checking the rotation and adding force to my object in that matter, for instance my cube has a rotation of 36 degree and its pushing my cube "forward" like it only has a rotation of 30 degree.

2: If we ignore my last matter and add some relative force to my Cube regardless of its rotation i noticed that if i were to release my key to go forward it slows down, now that ofcourse is supposed to happen but: instead of moving forward and slowing down, it moves forwards and slows down with a little offset to the left or right (yes it makes a little curve sideways).

If my explanations aren't clear enough or you would like some visual material to see, just drop a comment and ill create a video of my problem occuring.

Greetingz, TheMPC

AddRelativeForce does work in the manner that you describe.

What may be causing the small amounts of erratic movement are the cube's contact points with the ground.

Because your cube is actually scraping along the ground (unless you've turned off gravity), it may be that at certain times during its motion, your cube is actually in contact with the ground with fewer than all 4 of its bottom points. If this occurs, the friction against the ground may asymmetrical and cause your object to move in a direction other than its forward direction.

If you set both the cube's and the ground plane's colliders to use the preset "Ice" physic material, there will be no friction between the objects, and you should find your object moves exactly in the directions that you expect (although it will continue to slide across the surface when you let go of the keys!).

To solve this, you can then turn up the "drag" value on the cube's rigidbody to simulate the friction it would normally have with floor.

AddRelativeForce is relative to the object's rotation, AddForce is on the world axis.

If you use Get Axis then it'll be analogue movement (you can print the values or display them on the inspector to see) depending on how sensitive you've got the input manager set to. Try using GetKey instead to get digital input.

The drag values of the rigid body dictates how it'll behave like air resistance, try setting it to 0 for perfect straight line travel, it will keep moving forever though. Also gravity might be pulling it down.

Another thing which may be causing the drift might be that the objects centre of gravity might have moved if the objects is an odd shape or has moving children.