How to do ball movement depending on Iphone Orientation

Hi All,

How to do ball movement depending on Iphone Orientation

Thanks

There is an example in the iPhone examples of how to roll a ball.

Here’s some code I wrote to demonstrate this exact thing to someone…

(I think its a heavily documented and slightly re-written ball tutorial, I don’t remember)

// How much force we apply to the ball when we tilt the device
var tiltforce = 1.0;

// How high should we let the ball jump when the device is shaken?
var jumpHeight = 5.0;

// Time in seconds between allowable jumps
var jumpDelay = 0.75;

// Sensitivity
var kEraseAccelerationThreshold = 3.0;

// A private variable, because there is no need for it to be editable in the Inspector
private var jumpAllowed = true;

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

	// Lets assume that device is held parallel to the ground
	// and Home button is in the right hand
		
	// Remap device acceleration axis to game coordinates
	// 1) XY plane of the device is mapped onto XZ plane
	// 2) rotated 90 degrees around Y axis
	dir.x = -iPhoneInput.acceleration.y;
	dir.z = iPhoneInput.acceleration.x;
	
	// clamp acceleration vector to unit sphere
	if (dir.sqrMagnitude > 1) dir.Normalize();
	
	rigidbody.AddForce(dir * tiltforce);
}

function Update()
{
	if (jumpAllowed) checkForShake();
	if (Input.GetMouseButtonDown(0)  jumpAllowed) jumpBall();
}

function checkForShake()
{
	var accel = iPhoneInput.acceleration; 
	
	var x = accel.x; 
	var y = accel.y; 
	var z = accel.z; 
	    
	var length = Mathf.Sqrt(x * x + y * y + z * z); 
	    
	if (length >= kEraseAccelerationThreshold) { 
		// Send the ball into the air
		jumpBall();
	   }	
}

function jumpBall()
{
	rigidbody.velocity.y = jumpHeight;
	
	 // Do not allow another jump (or they'll be able to jump the ball out of the level)
     jumpAllowed = false;
     
     // Yield for the jumpDelay
     yield WaitForSeconds(jumpDelay);
     
     // Re-enable jumping
     jumpAllowed = true;
}

Hey this is really cool thanks for posting!