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
0
Question by Pablethe · Sep 14, 2017 at 11:40 PM · movementmovement scriptaccelerometer

Accelerometer

Hi friends!!

I am finalizing details of my first game for Android and I had some doubts.

I am making a game in which the movement is with the accelerometer. The inconvenience is that when I'm on the bus or if the device vibrates (a message arrives), this interferes with the player's movement. It moves crazy everywhere, and the gaming experience becomes very bad. I need to soften this movement a bit to improve that experience. Any suggestions?

THANKS A LOT!

My script movement.

public class MovimientoPersonaje : MonoBehaviour {

  [SerializeField]
  public float speed = 10f;
  public SpriteRenderer spriteRotar;
  public Rigidbody2D boxDudeRigid;
 
  public float variablePosicion = 0f;
 
  MatrixCalibrate calibration;
 
  public void MovimientoAcelerometro () {
 
      Vector3 dir = Vector3.zero;
      dir.x = (Mathf.Abs (_InputDir.x) > 0.07f) ? _InputDir.x : 0;
 
      if(dir.sqrMagnitude > 1)
          dir.Normalize();
 
      dir*= Time.deltaTime;
      transform.Translate(dir * speed);
 
      if(_InputDir.x < -0.02){
          spriteRotar.flipX = true;
      } 
      if(_InputDir.x > 0.02){
          spriteRotar.flipX = false;
      }
  }
  //Method for calibration 
 
  //Method to get the calibrated input 
  Vector3 getAccelerometer(Vector3 accelerator){
      Vector3 accel = this.calibration.calibrationMatrix.MultiplyVector(accelerator);
      return accel;
  }
  //Finally how you get the accelerometer input
  Vector3 _InputDir;
 
  // Use this for initialization
  void Start () {
      calibration = GameObject.Find ("Calibrate").GetComponent<MatrixCalibrate> ();
  }
  // Update is called once per frame
  void Update () {
      _InputDir = getAccelerometer(Input.acceleration);
      //then in your code you use _InputDir instead of Input.acceleration for example 
      //transform.Translate (_InputDir.x, 0, -_InputDir.z);
      MovimientoAcelerometro ();
  }

}

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 Pablethe · Sep 15, 2017 at 06:21 PM 0
Share

Any Answer?

2 Replies

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

Answer by Bunny83 · Sep 17, 2017 at 09:33 AM

The accelerometer is not ment to detect the orientation of your device. It's often used / abused for orientation sensing but it has exactly the issue you're describing. The accelerometer detects the linear acceleration of the device in each direction. The reason why you can actually use it for orientation sensing is that we have gravity. If you're not moving the device you have a constant acceleration of 1g downwards. However as soon as you accelerate the device in any other direction the acceleration get mixed with gravity.

Almost all modern mobile devices have a seperate gyroscope. The gyroscope does not detect linear acceleration but angular acceleration and integrates it to get an orientation value. This is (more or less) not affected by any linear motion.

Note that some devices do not have a gyro and others may simulate a gyro using the acceleometer.

Smoothing the accelerometer usually doesn't help much. It either adds too much latency to the input so it becomes unplayable or it's not enough and you still get problems with tiny accelerations.

Though if you want to smooth it, the best way would be a PID controller as you can fine tune how much the linear (P), how much the integral (I) and how much the differential (D) component affect the output. You probably want to keep D low or equal to "0". If you use P = 1 and I = 0 you get no change at all. You want to use something in between, for example P = 0.5 and I = 0.5.

A simple PID controller example has been posted by Andeeeee on the forum

When you use this PID script you can simply declare a public variable of type PID in your script and tweak the values for pFactor, iFactor and dFactor in the inspector.

To use it you would do something like this:

 public PID accelerometerPID = new PID(0.5f, 0.5f, 0f);
 private float accelerometerCurrent = 0;
 
     // Inside "MovimientoAcelerometro"
     Vector3 dir = Vector3.zero;
     accelerometerCurrent = accelerometerPID.Update(_InputDir.x, accelerometerCurrent, Time.deltaTime);
     dir.x = (Mathf.Abs (accelerometerCurrent) > 0.07f) ? accelerometerCurrent : 0;

 


Comment
Add comment · Show 3 · 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 Pablethe · Sep 18, 2017 at 12:41 AM 0
Share

Thanks for the explanation, it really is very useful.

But I'm having trouble integrating PID into my code. Basically I have two problems. 1) I can not declare PID, Unity generates error, does not recognize "PID" in:

 public PID accelerometerPID = new PID(0.5f, 0.5f, 0f);

2) I do not know what to replace in my code, for the part that you put me. Could you integrate the code in more detail?

$$anonymous$$any thanks. I really appreciate your help.!!!

avatar image Bunny83 Pablethe · Sep 18, 2017 at 04:13 AM 0
Share

Uhm, have you actually downloaded the PID controller that Andeeeee wrote? Of course you have to copy that script into your project.

About how to integrate it, i already posted all the changes you need in your script. Of course the two variables are defined in your class next to your other variables and the 3 lines of code i posted should replace the first two lines in your "$$anonymous$$ovimientoAcelerometro" method.

avatar image Pablethe · Sep 19, 2017 at 10:33 PM 0
Share

Works!! Thanks a looooooooooooooooooooooooooooooooooooooot!

avatar image
0

Answer by Animatick · Sep 18, 2017 at 09:46 AM

I have done some work using the accelerometer, I'm not saying buny89's post was wrong, but here's another approach that may work for you.

     public float rotationSpeed = 1.85f;
     const float ROTATE_AMOUNT = 2;
     private Quaternion rot;
    
     void OnEnable()
     {
         //used for android input
         rot = transform.rotation;
     }
 
     void Update()
     {
         float tiltValue = GetTiltValue();
         Vector3 oldAngles = transform.eulerAngles;
         transform.eulerAngles = new Vector3(oldAngles.x, oldAngles.y, 
        oldAngles.z + (tiltValue * ROTATE_AMOUNT));   
     }
 
     float GetTiltValue()
     {
         const float TILT_MIN = 0.05f;
         const float TILT_MAX = 0.2f;
        
         // Work out magnitude of tilt
         float tilt = Mathf.Abs(Input.acceleration.x);
         
         // If not really tilted don't change anything
         if (tilt < TILT_MIN)
         {
             return 0;
         }
 
         float tiltScale = (tilt - TILT_MIN) / (TILT_MAX - TILT_MIN);
        
         // Change scale to be negative if accel was negative
         if (Input.acceleration.x < 0)
         {
             return -tiltScale;
         }
         else
         {
             return tiltScale;
         }
     }

I hope this may help.

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

103 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 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 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 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

Implementing Counter-Movement 0 Answers

Touch buttons for step movememt 3 Answers

MouseLook character is acting wierd 1 Answer

How to drag a game object with a mouse (along x axis)? 1 Answer

How do I stop my character from moving in a circle upon destination arrival? 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