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
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();
}