Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by xmediagrafx · Sep 27, 2011 at 05:52 PM · inputaccelerometerinput.getaxisracing

How to convert Input.GetAxis to Accelerometer control?

Trying to convert this script for mobile use. How can I change the input.GetAxis statements to recognize accelerometer control rather than keyboard, I have tried a couple ways and cant get it, can someone get me over this hump? THANKS!!

 // A very simplistic car driving on the x-z plane.
     
     var speed : float = 10.0;
     var rotationSpeed : float = 100.0;
     
     function Update () {
     // Get the horizontal and vertical axis.
     // By default they are mapped to the arrow keys.
     // The value is in the range -1 to 1
     var translation : float = Input.GetAxis ("Vertical") * speed;
     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
     
     // Make it move 10 meters per second instead of 10 meters per frame...
     translation *= Time.deltaTime;
     rotation *= Time.deltaTime;
     
     // Move translation along the object's z-axis
     transform.Translate (0, 0, translation);
     // Rotate around our y-axis
     transform.Rotate (0, rotation, 0);
     }
Comment
Add comment · Show 1
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 xmediagrafx · Sep 27, 2011 at 05:59 PM 0
Share

i think its just my syntax when i try, should be these in place of input.getaxis:

"input.acceleration.y" for vertical "input.acceleration.x" for horizontal

Am I on the right track? haha:)

2 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by aldonaletto · Sep 27, 2011 at 09:30 PM

You're not so far! According to the Input script reference, in mobile devices Unity assumes Y parallel to the long side, X parallel the short side and Z pointing to the user, like this:

alt text

You can do the following: when the game starts, save the current Input.acceleration as a zero reference; during the game, subtract Input.acceleration from this reference and use the resulting X and Y to feed the Horizontal and Vertical axes (or vice versa, if the button is at the right side during the game):

 var zeroAc: Vector3;
 var curAc: Vector3;
 var sensH: float = 10;
 var sensV: float = 10;
 var smooth: float = 0.5;
 var GetAxisH: float = 0;
 var GetAxisV: float = 0;
 
 function ResetAxes(){
     zeroAc = Input.acceleration;
     curAc = Vector3.zero;
 }
 
 function Start(){
     ResetAxes();
 }
 
 function Update(){
     curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
     GetAxisV = Mathf.Clamp(curAc.y * sensV, -1, 1);
     GetAxisH = Mathf.Clamp(curAc.x * sensH, -1, 1);
     // now use GetAxisV and GetAxisH instead of Input.GetAxis vertical and horizontal
     // If the horizontal and vertical directions are swapped, swap curAc.y and curAc.x
     // in the above equations. If some axis is going in the wrong direction, invert the
     // signal (use -curAc.x or -curAc.y)
 }



iphoneaxes.png (102.3 kB)
Comment
Add comment · Show 9 · 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 xmediagrafx · Sep 27, 2011 at 09:44 PM 0
Share

Thank you:) I see now, haha:)

avatar image Gabryel · Apr 25, 2012 at 07:31 AM 0
Share

Thank you .. this helped me a lott :)

avatar image ganesh.pingale · Apr 12, 2013 at 11:48 AM 0
Share

but how to lerp value? $$anonymous$$y car getting extreme turn while I slightly tilt my device.

avatar image aldonaletto · Apr 18, 2013 at 09:57 AM 0
Share

This code already includes a "lerp filter": curAc retains the smoothed value of acceleration. You can control the smoothness with the variable smooth (the greater, the smoother), but I suspect that your problem may be the sensitivity: try smaller values for sensH and/or sensV.

avatar image Ankit Priyarup · Jun 16, 2013 at 04:16 PM 0
Share

well i am beginner can u help me in my 3d $$anonymous$$obile racing game i want the script for vertical control only.

Show more comments
avatar image
-2

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

alt text


accelerometer-icon.png (175.6 kB)
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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to convert Input.GetAxis to Accelerometer control? 1 Answer

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

Game doesn't recognize X-axis input from XBox 360 controller 1 Answer

How can I calibrate phone accelerometer? 0 Answers

S, D, and C keys not working together 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