"rigidbody.AddForce" forcing a default direction?

Hi there,
I’ve encountered a problem using rigidbody.AddForce. Not sure if it’s just me or maybe a bug?! I’ve also attached a very basic project. Anyone else encounteres this?:

When using the script below (extracted from rolling-a-ball) on a rigidbody, a force is applied via acceleration but unfortunately it’s not only acceleration “forcing” the rigidbody. Besides gravity and “var force” it seems as if there is kind of a default force with its own(!) direction affecting the rigidbody even when I put my iPod on the table not moving at all. This ‘default’ force seems to depend on the “var force”. Means: When using the accelerometer to control the red sphere there will be one (!) direction with a stronger force than in other directions. So controlling the sphere is definitly unbalanced. I would really appreciate some help in this. BTW I’m not a coder :slight_smile:

public var force : float= 300;
public var simulateAccelerometer : boolean = false;
public var forceZ : float = 0.32;

public var showTimer : GUIText;

public var objDrei : GameObject;
public var objZwei : GameObject;
public var objEins : GameObject;

var playing : boolean;
var startTime : float;
var countdownValue : int;
var achievedTime : float;


var currentMillisecond : int;
//rigidbody.AddForce(Vector3.zero);

function Start()
{
	// make portrait view
	iPhoneSettings.screenOrientation = iPhoneScreenOrientation.Landscape;
	startTime = Time.time;
	countdownValue = 3;
	showTimer.text = "";
	rigidbody.useGravity = false; 
	
	objDrei.gameObject.active = true;
	achievedTime = 0;
	playing = false;


}

function FixedUpdate () {
	
	var dir : Vector3 = Vector3.zero;

	if( (Time.time-startTime) > 1  countdownValue == 3 ) {
		countdownValue = 2;
		objDrei.gameObject.active = false;
		objZwei.gameObject.active = true;
	} else if ( (Time.time-startTime) > 2  countdownValue == 2 ) {
		countdownValue = 1;
		objZwei.gameObject.active = false;
		objEins.gameObject.active = true;
	} else if ( (Time.time-startTime) > 3  countdownValue == 1) {
		countdownValue = 0;		objEins.gameObject.active = false;
		playing = true;
		currentMillisecond = 10;
		showCounter();
		rigidbody.useGravity = true;
		//rigidbody.AddForce(-98.1,0,0); 
	}
	

	
	if(playing) 
	{
		if (simulateAccelerometer)
		{
			// using joystick input instead of iPhone accelerometer
			dir.x = Input.GetAxis("Horizontal");
			dir.z = Input.GetAxis("Vertical");
			//Physics.gravity = Vector3(0, -1, 0);

		}
		else
		{

			dir.x = -iPhoneInput.acceleration.y;
			dir.z = iPhoneInput.acceleration.x;
			dir.y = iPhoneInput.acceleration.z;


			// clamp acceleration vector to unit sphere
			if (dir.sqrMagnitude > 1)
			dir.Normalize();
			
			//if (dir.z <= forceZ)
			//	{	dir.z = 0; }
				
					
			/*if (-forceZ*0.25 < dir.z  dir.z < forceZ*0.25) {
				dir.z = 0;
			}
			
			if (-forceZ*0.25 < dir.x   dir.x < forceZ*0.25) {
				dir.x = 0;
			}

			}*/		
		}
		
		rigidbody.AddForce(dir * force);


	}
}

function showCounter() {
	yield new WaitForSeconds (0.001);
	updateTimer();
}

function updateTimer() {
	if (playing == true) {
		achievedTime = Time.time - startTime - 3 +0.001*currentMillisecond;
		showTimer.text = achievedTime.ToString("#.###");
		
		if(currentMillisecond>1) currentMillisecond--;
		else currentMillisecond=10;
	}
	
	
	showCounter();
}

197034–7170–$gravityonoff_247.unitypackage (9.87 KB)

Does AddRelativeForce() do what you want?

I tried that. But when I replace AddForce with AddRelativeForce the sphere behaves “funky”, but I’m looking for a correct control of the sphere. Actually, as I described before I can control the sphere but the force is much stronger into ‘one’ direction than in other directions.

…so I checked against the rolling-a-ball project only replaced the board against a flat cuboid. And its the same problem here, when your iphone/ipod is not moving at all and working with larger forces you will clearly see that the ball starts to accelerate in its prefered direction like a race car. When moving the iPod you can control the ball but in the “prefered” direction the force ist stronger compared to other directions.

Can anyone confirm this, please? Or point me to a workaround?

Hello!

my hypothesis is that the problem is that the accelerometer even if its on a table without movement, it measures somehow little values, so that might the problem.

If you want to check my hypothesis just Debug.Log your acelerometer when you put it on your table :slight_smile:

Hope it helps! :wink:

I guessed already that the accelerometer is producing some “noise” maybe a little bit comparable to noise in motion capture data. But noise would have kind of a chaotic structure: my impression is that the force is pointing into a precise direction something like Vector(1,0,1). You can simply test it by using the roll-a-ball example replacing the board with a flat surface (make it very large). You will see the ball continously accelerating on this vector. The problem is that this force adds to already applied forces with the effect of unbalanced behaviour of the ball → acclerates stronger in this direction. Not sure if this is related: but then I use the standard bouncy material the sphere jumps higher and higher.

People say truth, hardware may produce some noise. It depends on actual hardware, it’s drivers and lots of other non-unity things.