- Home /
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();
}
rigidbody.AddForce(dir * force);
}
Answer by duck · Apr 20, 2010 at 09:14 AM
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;
This can not be answered any better. Duck has it dead on.
Answer by RadhaUnity3d · May 09, 2013 at 12:22 PM
Use this since IphoneInput is old,,,,,,,,,
dir.x = Input.acceleration.y; dir.z = -Input.acceleration.x;
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
make a wedge moveable 0 Answers
How to roll a ball? 1 Answer
iPhone ball roll problem 2 Answers
Sphere rotation in moving direction 1 Answer