- Home /
How to make simple tilting function on the iPhone
Hi,
I'm trying to create a simple tilt function using the iPhone's accelerometer in Unity. I've looked at a lot samples but can't get it to work the way I want. My App basically has a 2D character. What I'm trying to do is:
//pseudocode if(tiltleft > 0) { //do something }
if(tiltright > 0) { //do something }
Could someone advice me on how to approach this or point me to a good tutorial on accelerometers please?
Thanks in advance,
Hakimo
Answer by Thom Denick · Jul 28, 2010 at 05:46 AM
There's a decent example for multi-dimensional tilt functionality in the iPhone Unity documentation, but I actually just messed around with getting this working in my project last night.
I think we have the same problem - apply the tilt along X to the X coordinate of our character in a 2D plane. Here's how I got it working... This is in C# and is based on the iPhone controls documentation.
public float TiltSpeed = 5.0f;
void FixedUpdate() { float tiltAngle = iPhoneInput.acceleration.x;
tiltAngle *= Time.deltaTime;
tiltAngle *= TiltSpeed;
// Move object - Change this to transform.position if you don't have a rigid body on your char.
rigidbody.AddForce(tiltAngle, 0, 0);
}
Obviously, you need to test this on the device or with Unity Remote. I actually have a script that displays my devices tilt amount onGUI so I can see precisely what kind of X values the device is returning.
Wow, that looks easier than what I have tried. I even got to see the values as well. Although I'm a bit stuck at the moment with the if conditions. I'm using print(transform.position.x) to get the values. I tried to use: if(transform.position.x == -0.1) { //do something } Unfortunately nothing works. What value should the conditions be? I even used tiltAngle but nothing happens.
Thanks again,
It's alright, manage to do something. I didn't check the value properly for it to make any effect. Thanks again. Also, where did you learn about accelerators? The one example I followed earlier made so many variables that I didn't quite understand what it was doing :P
This is all modified from the Help documentation in Unity under the iPhoneInput script reference. I do think the example could have been simpler, but I guess they wanted to show that generally for a ball rolling around you would want to be polling at least two coordinates.
Thanks again. Just wanted to understand more about the coding; How does the float TiltSpeed effect the tilt? I can't seem to see any dramatic changes. Also, when you assigned TiltSpeed to tiltAngle, does it override the Time.deltaTime? I didn't know you can assign 2 values to a variable. Thanks again for the help :)
Hi Hakimo,
Float is a type of number that can hold decimals. Since the Input is a decimal number, it's perfect for holding the X Acceleration.
In the next two I'm multiplying the TiltAngle. $$anonymous$$ultiplying movement deltaTime is a way of ensuring that you move based on the time and not by Frames (which can vary by FPS). Second, I multiply by our tilt speed which is the variable that affects how much our character moves based on the tilt.
Answer by cregox · Jan 28, 2011 at 10:28 PM
As Smorpheus suggested, to use the accelerometer I've got the Controller.js
from Unity's Roll-a-ball iPhone example: http://unity3d.com/support/resources/example-projects/iphone-examples.html
Maybe it's not exactly what you needed, but I guess it's related enough, a good hint and good to document. It's quite simple and I even simplified it a little:
public float force = 9.8f;
void FixedUpdate () { dir.x = Input.acceleration.x; dir.y = Input.acceleration.y; Physics.gravity = dir * force; }