Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OneMadSquid · Oct 08, 2014 at 04:54 AM · inputmobileaccelerometer

Need GUIDANCE On Mobile Accelerometer Input Applied to Rigidbody

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:

 //movement
     void FixedUpdate ()
     {
 
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         rigidbody.velocity = movement * speed;
         
         rigidbody.position = new Vector3 
             (
                 Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
                 0.0f, 
                 Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
                 );
         
         rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
         //movement
     }

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MakeCodeNow · Oct 08, 2014 at 05:00 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by OneMadSquid · Oct 08, 2014 at 06:17 AM

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:

http://www.assetstore.unity3d.com/en/#!/content/8566

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MakeCodeNow · Oct 09, 2014 at 01:21 AM 0
Share

$$anonymous$$aybe, but I think you can figure it out yourself. Just look at the start values,

You would know you were applying a force if you were calling one of the apply force commands. Setting any property directly, like position or velocity produces results independent of mass, so it can't be a force.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Mobile Skater game question 1 Answer

how to move an object in 4 directions by tilting device when not parallel to the ground 0 Answers

accelerometer speed 1 Answer

A node in a childnode? 1 Answer

Bag Toss Unity Accelerometer 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges