- Home /
Accelerometer Auto rotate Issue
I have a movement script that works pretty well when I'm playing on Landscape Left, but since I'm building for iPad, it needs to auto-rotate, so I put in a different script to do that, but when I try to make my movement script rotate with it, my objects still move in the opposite direction when playing on Landscape Right. I have my script below, it's probably just an oversight on my part, but I just can't figure it out.
function FixedUpdate()
{
var dir : Vector3 = Vector3.zero;
if(DeviceOrientation.LandscapeLeft)
{
dir.x = -Input.acceleration.y*.5;
transform.position.x += dir.x;
}
else if(DeviceOrientation.LandscapeRight)
{
dir.x = Input.acceleration.y*.5;
transform.position.x += dir.x;
}
}
Answer by Cyb3rManiak · Apr 26, 2011 at 09:21 AM
Is there a chance that when you flip the device Input.acceleration gives you negative values, and you just cancel them out when you negate them because you expect to receive positive ones?... Try and show the acceleration values on a GuiText on the screen to debug it better.
I'm afraid it was a long time ago I fiddled with iDevices and Unity and I'm a bit rusty.
I've tried a lot of different variations of the movement for landscape right and displayed the output values, but no matter what I put, the output values always seem to be the same between landscape right and landscape left.
I can try and help you, but I think that without Unity for iPhone and working from memory alone without seeing some code it will be tricky. Have you tried looking at some example projects and see how other people deal with this issue? For example, http://unity3d.com/support/resources/tutorials/penelope is a ncie tutorial that might address this issue (As I said - I just don't remember... :/ ).
Any chance this is a silly thing like using transform.localPosition ins$$anonymous$$d of transform.position? Or using Transform.TransformDirection to get relative direction and not global direction?
Penelope didn't have anything about auto rotation and transform.localPosition didn't really do anything, I suspect because my object has no parent. As for the Transform.TransformDirection, could you possibly elaborate on how to use that? I'm not quite sure and when I just tried to replace my transform with that, it gave me an error saying an instance of Transform is required to access TransformDirection. I really appreciate the help though, this is just driving me crazy because it works in one orientation but not the other.
Okay, I know what's wrong now, it never goes into my else if statement to check for if the orientation is landscape right. Does anyone know why this would happen or how to fix it?
Answer by Cyb3rManiak · May 01, 2011 at 08:52 AM
See, this is what I mean I'm rusty... Can't believe I missed it.
You're not checking the current orientation in your IF clause... DeviceOrientation.LandscapeLeft is an enum. It's like a constant value that you check against something. In your case: Input.deviceOrientation
:)
if you print out the value of DeviceOrientation.PortraitUpsideDown, for example, you will always get the same value, and it will never change. The important thing is which of these values is in Input.deviceOrientation at the moment, and it can only be one of the values in that enumeration. Check the example in the link if it's confusing.