# iphone roll a ball game

I'm just wanting to do something simple like hold the iphone sideways and roll the ball depending on where I tilt the iphone. There is a roll a ball tutorial or resource but the controls are completely backwards for it. I tilt forward and it rolls backward.

This is what I want:

I want to tilt the iphone and the ball gains momentum as I continue to tilt the phone.

Bounce off of the walls that I put in it.

I know this is very simple but I'm pretty new to everything since I switched from torque script so help convert me over to unity :D

Here is the code I'm using:

``````public var force:float = 1.0;
public var simulateAccelerometer:boolean = false;

function Start()
{

iPhoneSettings.screenOrientation = iPhoneScreenOrientation.Landscape;
}

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

if (simulateAccelerometer)
{
dir.x = Input.GetAxis("Horizontal");
dir.z = Input.GetAxis("Vertical");
}
else
{

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

if (dir.sqrMagnitude > 1)
dir.Normalize();
}

}

``````

If the ball rolls in exactly the opposite direction, simply add a minus sign ("-") in front of the force vector that you're applying.

If the ball isn't gaining momentum as you tilt the phone, perhaps you're not using AddForce - which should continue to add force each frame while the phone is tilted.

Another method of implementing a ball rolling game is to adjust the gravity vector based on the phone's tilt angle - perhaps your code uses this? In this case, to switch the direction that the ball rolls, you'd want to only negate the X and Z gravity direction, but not the Y value.

Without seeing any of the code you're actually using, it's hard to be any more specific about it, so if none of these ideas work for you, edit your answer to show some of the code you're currently using to make the ball move.

Edit: given your code sample above, you could try just invert the values from iPhoneInput like this, to reverse the ball motion:

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

``````

Use this since IphoneInput is old,

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