- Home /
iPhone ball roll problem
I'm using the Roll-A-Ball script to have a ball roll around a small area and I am having two problems:
1) The ball is rolling around in a box made of flattened cubes. All but the bottom has mesh render removed so they can't be seen. Is there any reason why, when the tilt angle is too great, the ball gains enough momentum to launch out of this box?
2) I want to change the existing script so that when tilted, the ball has a set speed. I need the player to be able to switch directions quickly and this seems impossible as the physics (or accelerometer?) are causing the ball to just slow down and then eventually speed up again when tilt direction is changed.
I think if I could fix problem 1) then I could just increase force enough so that changing direction wouldn't take so long...
Here is the code example:
function Start() { iPhoneSettings.screenOrientation = iPhoneScreenOrientation.Landscape; }
function FixedUpdate () {
var dir : Vector3 = Vector3.zero;
dir.x = -iPhoneInput.acceleration.y;
dir.z = iPhoneInput.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
rigidbody.AddForce(dir * force);
}
This is looking all funny on my screen, I think I've pressed something wrong trying to copy the code in..
Any help would be greatly appreciated ^_^
If you don't want the ball to bounce at all you could set the y axis to zero in the update function.
Answer by B3NN · Jul 21, 2010 at 12:37 AM
Turns out the that the walls used to enclose the ball were not thick enough to contain it.. Just a matter of increasing the thickness. :) This way, I don't have to restrict the ball to just the Y axis and don't need to slow down the fixedTimeStep.
This also allowed me to increase the balls movement speed enough to have the effect I wanted! Was a minor headache, but it's done now :) Thanks for the help above, also :)
As for making wall thick, you might want this ins$$anonymous$$d: http://www.unifycommunity.com/wiki/index.php?title=DontGoThroughThings
Answer by Daniel 6 · Jul 13, 2010 at 06:55 PM
To prevent object's from going through each other, you could just decrease the fixedTimeStep in the physics settings (Edit->ProjectSettings->Physics).
I did try that, however slowing down the fixedTimeStep just slowed down the entire game.. i really wanted the ball to move quickly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064604im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
iphone roll a ball game 2 Answers
Device Orientation Issue 1 Answer
moving an object with accelerometer using unity iphone. 3 Answers
iPhone touch raycasting 1 Answer