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 nohedonist · May 16, 2013 at 12:32 PM · mobileaccelerometeraccelerationtiltmathf.clamp

Using Mathf.Clamp with Accelerometer to control movement speed has slow response time, how can I smooth it out?

Hi guys, first question on UA. Im a first year game design student with a moderate amount of coding experience, who is now undertaking a project for an internship, and my expertise is lacking ^^.

The situation: a 2D side scrolling game where the player's speed will be boosted if the mobile device is tilted in a particular direction.

I have never made a mobile game so I have been using a lot of example code and reverse engineering it to understand how to control the functionality.

ie. this is not my code, it was taken from an example and slightly modified to work in my circumstance.

Code is as follows :

 var zeroAc : Vector3;
 var curAc : Vector3;
 var sensH = 15.0;
 var sensV = 15.0;
 var smooth = 0.0;
 var getAxisH = 0.0;
 var getAxisV = 0.0;
 
 var isRight=false;
 var isLeft=false;
 var isUp=false;
 var isDown = false;
 
 //*******************This is the part of the code which i need help with***************
 function Update () {
 
    curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
    getAxisV = Mathf.Clamp(-curAc.x * sensV, -1, 1);
    getAxisH = Mathf.Clamp(-curAc.y * sensH, -1, 1);
 
 if(getAxisH==-1)
    { isLeft = true; }
 
 else if(getAxisH==1)
    { isRight = true; }
 
 else {isLeft = false;                  
       isRight = false;}
                               
 if(getAxisV==1)
    { isUp = true; }
 
 else if(getAxisV==-1)
    { isDown = true; }
 
 else {isUp=false;
      isDown=false;}
 }
 
 ///*********The following is pseudo-code for the effect of tilting the device***********
 
 if (isRight){
               Player Speed = doubled;
 }
 else {
               Player Speed = normal;
 }


Now the effect of doubling the speed works just fine (unless I copied the code into this question wrong), however the issue is with the clamping.

When the device is tilted sufficiently to the right the value getAxisH hits 1 and is correctly calmped at that value.

Now 2 issues arise. First of all, the range at which var isRight is no longer true is very small and quite hard to find without over shooting into isLeft, which is undesirable in this case. Obviously i could extend the range but then we would have to tilt further to go into isRight, also undesirable.

The second issue is that the response time between the device tilting and the game responding is quite big, thus the effect is delayed substantially, which all but defeats its purpose in this game.

So in conclusion my questions are:

1. Is there a convenient way of reacting instantly to direction of tilt, and not magnitude in one direction or another?

2. Is the lag inherent to accelerometers in mobile devices, or is there something up with my nexus 7/laptop that the response time is something like half a second (massive for a reaction based scrolling game)?

As I say, I am quite inexperienced with game programming at this point, therefore I humbly welcome all criticisms and pointers, related to my questions or otherwise, even if it is just to say that the question is stupid ^^ I also apologise if the answer is self-evident and yet eludes me. I have terrible sub-nasal vision.

Thank you all for reading and I hope someone can help.

Cheers

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

0 Replies

· Add your reply
  • Sort: 

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

13 People are following this question.

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

Related Questions

Get absolute acceleration data from accelerometer/gyroscope but neglect rotational acceleration 1 Answer

Moving object with tilting the device. 1 Answer

Move if mobile tilts in positive Y direction 0 Answers

Networking Accelerometer uses wrong axis 0 Answers

Using acceleratorometer, manipulate the data to trick phone into thinking gravity is pointing in a different direction than down. 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