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 N1warhead · Nov 01, 2014 at 06:00 PM · androidmobileaccelerometer

accelerometer speed

Hey guys I was looking at the accelerometer tutorial that Unity made.

I've been trying to understand how to limit the speed. Right now it shoots off at a thousand miles per hour if I tilt it more than a centimeter.

There's no much to look at, but I'm only wanting to use the accelerometer for the X axis.

Here is the basic code that I have.

 public class Controls : MonoBehaviour {
 
     // Variables dealing with Speed and boost.
 
     public float speed;
     public int sideSpeed = 1.0;
 
 
 
     void Start(){
 
     }
 
     // Update is called once per frame
     void Update () {
         //Use this for constant forward movement
         transform.Translate (Vector3.forward * speed * Time.deltaTime);
         //Use this for the accelerometer for X Axis.
         transform.Translate (Input.acceleration.x, 0, 0 * sideSpeed * Time.deltaTime);
 
     }
 
 
 }


I've tried doing it with Addforce, but even if I change the force to 50000 it just moves in slowmotion.

I've seen stuff about clamps, but I don't understand the clamping for this. I'm new to mobile development.

Please help me.

Thank you!

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

1 Reply

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

Answer by NickP_2 · Nov 01, 2014 at 06:09 PM

Use a rigidbody with addforce in a FixedUpdate() function instead of the Upate function.

Mathf.Clamp(float value, float min, float max) just returns a value between 2 given, if the value is lower than the min, the value will be min, if it's higher than max, it'll be max. for example:

 float value = Mathf.Clamp(10, 2, 3);// value will be 3, because 10 is bigger than 3.
 float value = Mathf.Clamp(10, 20, 30);// value will be 20, because 10 is smaller than 20.

as for the rigidbody, it should look something like this:

 void FixedUpate()
 {
     Vector3 force = new Vector3(Input.acceleration.x, 0, 0) * sideSpeed;
     rigidbody.AddForce(force);
 }

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 N1warhead · Nov 01, 2014 at 06:14 PM 0
Share

I did do it in a Fixed Update, but I didn't do it the way you did!

THAN$$anonymous$$S IT WOR$$anonymous$$S!!!!!!!!!!!!

I'm used to PC stuff so this is a bit different for me. But I did it and it works GREAT!

You are a lifesaver!

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

27 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 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 avatar image

Related Questions

A node in a childnode? 1 Answer

Standard Mobile Joysticks are locking up. 0 Answers

Android double jump not working correctly 1 Answer

Using the accelerometer to move a player as the real user walks 2 Answers

How do I get Unity to stop making changes to AndroidManifest.xml? 2 Answers


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