- Home /
Accelerometer Quick Question(Moving Up and Down?)
So lately I've been messing around with the Accelerometer code in the tutorials for Unity, and I was wondering how can I get my game object to move up and down on the Y axis instead of the X? You see I use this little piece of code so far:
void Update() {
transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
}
}
So this moves my game object right and left when I tilt the device right or left. Which is amazing but I'm trying to move it up or down when I tilt up or down. Changing the first Input.acceleration from x to y only changes the controls, but doesn't change the movement of the game object. I know this is a very basic question, so scold me if you must. Also don't worry I've checked other answers and they seem to use a different method than the one in the tutorial(the one I'm using), so it makes it harder to understand fully without copying someone else's code.
Just a few ideas you can look into -- I have modest experience working with handheld devices and accelerometers, but not a whole lot, so I can't guarantee specific results, but http://docs.unity3d.com/ScriptReference/Screen-orientation.html should have some information handy for screen orientation.
Additionally, it may vary from device to device, but when I worked with it, the phone I used would automatically adapt its accelerometer axes relative to the current orientation, so once you find the right combination of Input.acceleration and transform.Translate for each axis, you should be set.
That said, to simply speculate a little bit, between your initial post and the first response to it, my guess would be that you might be looking for something like transform.Translate(-Input.acceleration.x, Input.acceleration.z, 0);
That would make it where a camera pointed forward along the Z axis towards the game scene would use left and right tilting to move on the X axis, then forward and back tilting would move up and down.
Sadly the code that you provided doesn't work for me. $$anonymous$$y Player just goes straight down and I can't see him anymore. No matter what I try to do it won't let me move back up. Anyways I'm looking into the screen orientation you posted. So for now, if you can help me anymore in any way possible, please share your knowledge if you want.
Answer by ByteSheep · Oct 02, 2014 at 08:27 AM
The Translate function takes three parameters which are:
transform.Translate(x, y, z);
Well actually it has a couple other optional parameters (see docs)
So if you want to move the object along its y axis (up and down) you will probably want something along the lines of:
transform.Translate(0, Input.acceleration.y, -Input.acceleration.z);
The first parameter is 0 because you don't want the object to move along its x axis.
The second value (objects y) is being modified by the y acceleration value.
Here's a link to the documentation: http://docs.unity3d.com/ScriptReference/Transform.Translate.html
Hope that helped :)
Thank you for trying to help, I appreciate it. Sorry but I forgot to mention that my test game plays with landscape left only. So when I play the game with your code, my game object goes all the way to the left. The only way for me to move, is if I flip my device to where the button is facing down, then I have to turn right to move forward. So I didn't even move along the y axis. only the x.
What I'm trying to do, is in landscape left mode tilt up to go up, and tilt down to go down. I've heard that it's supposed to adjust to the screen mode, but it may not be the case here. I'll probably find the answer in the docs somewhere.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
New to Unity, Need Help. 1 Answer
How To Add PlayerPrefs Scores? 1 Answer
Facebook SDK and ADColony Compatible in iOS? 2 Answers
Player lives script help 1 Answer