- Home /
 
Using the accelerometer to move left and right on one axis
Hi there, I'm extremely new to UnityIphone and I'm trying to do something that I would consider very simple.
I'm trying to make a object 'strafe' left and right depending on the tilt of the accelerometer, I don't even want velocity to come into play, simply move left at a constant speed in tilted left, and visa-versa.
Thanks - Caius
Answer by Billamu · Aug 09, 2010 at 11:04 PM
var speed : float = 10; var moveThreshold : float = .2;
 
               private var movex : float; private var iPx : float; 
 
               function Update() { movex = 0; iPx = iPhoneInput.acceleration.x;
 
                if (Mathf.Abs(iPx) > moveThreshold)
 {
     movex = Mathf.Sign(iPx) * speed;
     transform.Translate(movex,0,0);
 }
 
               } 
 
               
               The variable moveThreshold is used so that your object will remain still when the device is upright and will only move when you lean the device enough.
Mathf.Sign() outputs a +1, 0 or -1, whereas Mathf.Abs() returns a positive number only.
http://unity3d.com/support/documentation/ScriptReference/Mathf.Sign.html http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
somebody tell me how to put this in my script. i try to copy but when i paste in scrip they don t work. scrip is going red help
Answer by dilmer · Sep 12, 2013 at 06:58 AM
Check out this control I wrote which may help you out.
Guys,
I worked in a game recently which needed an accelerometer control for a spaceship I created which is now available in the App Store as well, during my programming I found myself using this control for other games. I would recommended as is super easy to integrate into your game.
Download Now from the Asset Store
![]()
If you wanted to advertise, it would be better after showing you could at least attempt to answer the question. No point trying to hold back other developers for a few dollars.
Your answer