So, I’ve just completed the Space Shooter tutorial and I’m really happy to have successfully created the game according to specs. Just for grins and giggles I decided to remake the game with my own art and sound. This came out fairly well. The thing is, I set my target to iOS because I wanted to attempt porting the game, which relies on WASD control input, for use on my iPad. I am stuck as can be though when it comes to changing the whole Input class over to receive accelerometer input. I watched the official video, and the code there is quite simple, except that it only applies to non-rigidbodys. My Player GameObject is a rigid body. The code used in the tutorial is:
I felt like I totally understood this when I ran through the tutorial, but when I look at the code in the script reference documentation for Acceleration Input I see all kinds of things I don’t recognize: “dir” and “cali” and such. I’m not asking for free code, but I can’t seem to find a good step by step tutorial. Can someone point me in the right direction? I don’t mind teaching myself through good documentation…I just can’t seem to find it. =-(
Long story short, I would like to know who to detect accelerometer input, and use that input to apply force to my rigidbody player GameObject.
First off, that code is not technically applying a force to the RB. It is just setting the position directly (i.e. teleporting in small steps). Anyway…
Out of the box, an accelerometer is not similar to a joystick. The accelerometer in the phone measures acceleration (changes in speed) along three axes that are relative to the device (i.e. the axes rotate when the phone rotates). Gravity is an acceleration that is always acting on the device (even when it’s not moving) and of course humans accelerate it when they move it around.
If you want an accelerometer to behave like a joystick, you need to pick two axes, record their values at start, and then think of changes from these axes as the joystick like values. The tricky part is knowing which axes to pick.
To help you out, I recommend adding some debug code to OnGUI that prints the three accelerometer axis values to the screen as text. That way you can see how the numbers change as you move the phone around, which will give you a good intuition for how the system works. From there, you should be able to figure out which axes to use, etc.
Excellent advice with the debug code. I am still a newb and I often forget how useful debug.log and such can be. Also, I found this:
Thinking I might be able to reverse engineer the controller script from this?
As for the code above not applying a force to the rigid body, I thought that applying force was the only way to move one. I came to understand that transform does not work with a physics based game object. The code above implies that force has been applied by referencing velocity, yes? You can’t have velocity without applying an external force. Am I way off?