• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by looMeenin · Dec 31, 2014 at 08:27 AM · c#javascriptfilteraccelometer

How to implement a low pass filter for accelerometer?

Here is the code I am using to move my character with accelerometer(no low pass filter):

 public float speed = 30.0f;
     private float accel;
     Vector2 dir;
 
 void Start () 
     {
     accel = Input.acceleration.x;
 
     }
     
 // Update is called once per frame
 void Update () 
 {  
     accel = Mathf.MoveTowards (accel, Input.acceleration.x, speed * Time.deltaTime);

     // map accel -Y and X to game X and Y directions:
     dir = new Vector3(accel, 0);
 
     // move the object at the velocity defined in speed:
     transform.Translate(lowPassValue * speed * Time.deltaTime, 0);
 }

I want to implement a low pass filter that I found in the Unity manuals here:http://docs.unity3d.com/Manual/MobileInput.html

Here it is(in UnityScript):

 var AccelerometerUpdateInterval : float = 1.0 / 60.0;
 var LowPassKernelWidthInSeconds : float = 1.0;
 
 private var LowPassFilterFactor : float = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds; // tweakable
 private var lowPassValue : Vector3 = Vector3.zero;

 function Start () {
     lowPassValue = Input.acceleration;
 }
 
 function LowPassFilterAccelerometer() : Vector3 {
     lowPassValue = Mathf.Lerp(lowPassValue, Input.acceleration, LowPassFilterFactor);
     return lowPassValue;
 }

Can someone help me implement this low pass filter into my original c# code?

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
2
Best Answer

Answer by hav_ngs_ru · Dec 31, 2014 at 10:11 AM

 using System.Collections.Generic;
 
 ...
 
 protected Queue<Vector3> filterDataQueue = new Queue<Vector3>();
 public int filterLength = 3; //you could change it in inspector
 
 void Start() {
    ...
    for(int i=0; i<filterLength; i++)
       filterDataQueue.Enqueue(Input.acceleration); //filling the queue to requered length
    ...
 }
 
 public Vector3 LowPassAccelerometer() {
    if(filterLength <= 0)
       return Input.acceleration;

    filterDataQueue.Enqueue(Input.acceleration);
    filterDataQueue.Dequeue();
 
    Vector3 vFiltered= Vector3.zero;
    foreach(Vector3 v in filterDataQueue)
       vFiltered += v;
    vFiltered /= filterLength;
    return vFiltered;
 }

didnt test it for typos, but idea is like this

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
2

Answer by michaelstv · Mar 14, 2016 at 02:04 PM

Hi

Here is another implementation, simpler IMHO as it doesn't use an array of past values: using UnityEngine; using System.Collections;

 public class LowPassTransformFilter : MonoBehaviour
 {
     Quaternion currentRotation, targetRotation;
     Vector3 currentPosition, targetPosition;
 
     // with these fields you can select whether to apply low pass on rotation or position only
     public bool applyOnRotation = true;
     public bool applyOnPosition = true;
 
     // This field controls the low pass value
     // Use 1 for no filtering, and a value closer to zero for more sluggish filtering 
     // (Note that zero would be invalid and freeze the transform)
     //
     [Range(0.1f, 1.0f)]
     public float lowPassFilter = 0.5f;
     public float LowPassFilter { set { lowPassFilter = value; } }
 
     // Use this for initialization
     void Start ()
     {
         currentRotation = targetRotation = transform.rotation;
         currentPosition = targetPosition = transform.position;
     }
     
     // Use LateUpdate in case other scripts also use LateUpdate to change the transform
     void LateUpdate ()
     {
         if (transform.rotation != currentRotation) {
             targetRotation = transform.rotation;
         }
 
         if (transform.position != currentPosition) {
             targetPosition = transform.position;
         }
 
         if (lowPassFilter < 1) {
             if (applyOnRotation) {
                 transform.rotation = Quaternion.Slerp (currentRotation, targetRotation, lowPassFilter);
             }
             
             if (applyOnPosition) {
                 transform.position = Vector3.Lerp (currentPosition, targetPosition, lowPassFilter);
             }
         }
             
         currentRotation = transform.rotation;
         currentPosition = transform.position;
     }
 }
 
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 caseyfarina · Jan 05, 2017 at 06:32 AM 0
Share

Thank you, Thank you, Thank you. I'd award 100 points if I could :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

How I can find the complete event of my animation 1 Answer

Movement script, moving up when moving positively in z axis 1 Answer

How to Move an object after it was reproduced by spawn 0 Answers

snap object to object in game view ? 1 Answer

How to change game object axis according to the camera rotation? 0 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