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
1
Question by ahmedbenlakhdhar · Dec 06, 2014 at 06:14 PM · camerapositionsmoothaccelerometermathf.lerp

How to smooth the accelerometer data values

Hello, I am trying to retrieve the acceleration only from the accelerometer and use it to change the camera position smoothly (the gyroscope is not involved).

Here is a piece of code attached to the main camera:

 void Update() { 
     float lerpAmount = 0.2f;
     Vector2 cameraTargetPos = (Input.acceleration*-20);
     Vector2 currentPosition = transform.position;
     float xPos = Mathf.Lerp(currentPosition.x, cameraTargetPos.x, lerpAmount);
     float yPos = Mathf.Lerp(currentPosition.y, cameraTargetPos.y, lerpAmount);
     transform.position = new Vector3(xPos, yPos, -10);
 }

As can be seen, I am using Mathf.Lerp to make the acceleration data smooth. The problem is that:

  • the smaller lerpAmount is, the more the vibration is noticed;

  • the greater lerpAmount is, the greater the latency is.

I am looking for help to see a better solution than using Mathf.Lerp. Thank you in advance.

Comment
Add comment · Show 3
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 hfpngaming · Jun 06, 2016 at 05:06 AM 0
Share

@ahmedbenlakhdhar Did you find the solution to your problem? If you did, would you $$anonymous$$d sharing your code it'd be really helpful. Thanks :)

avatar image ahmedbenlakhdhar hfpngaming · Dec 30, 2016 at 10:23 AM 0
Share

No sorry, I did not yet find a solution.

avatar image Jake0 · Dec 30, 2016 at 12:54 PM 0
Share

Have you tried the Time $$anonymous$$anager? Here you can set the Timestep. This is located under the Edit/Project Settings/Time.

I had a problem with camera vibration in my 2d game when the camera was following a game object. Vibration or Latency depending on the code. The vibration was fix on my project by changing the Fixed Timestep to .0001. I hope this helps. Jake

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by vivek.rawat · Dec 06, 2014 at 06:54 PM

hey try using Mathf.SmoothDamp instead of Mathf.Lerp

http://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html

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 ahmedbenlakhdhar · Dec 06, 2014 at 09:03 PM 0
Share

Thank you, this function seems to be the right way to make a smooth, but its result is exactly similar to $$anonymous$$athf.Lerp.

avatar image
0

Answer by Ouss · Dec 06, 2014 at 09:13 PM

You need to use a filter lowpass or highpass filter depends on your needs to filter noise from acceleromter data. Here their implementation, you feed a vector3 of the acceleration and get a filtered value:

public Vector3 highPass(Vector3 accVal) {

     Vector3 filteredAcc = lowPass(accVal);

     //resultat = Filtre passe bas - valeur actuelle de l'accéléromètre
     filteredAcc.x -= accVal.x;
     filteredAcc.y -= accVal.y;
     filteredAcc.z -= accVal.z;
     
     return filteredAcc;
     
 }

 public Vector3 lowPass(Vector3 accVal) {

     xFilt = accVal.x * kFilterFactor + xFilt * (1 - kFilterFactor) ;
     yFilt = accVal.y * kFilterFactor + yFilt * (1 - kFilterFactor) ;
     zFilt = accVal.z * kFilterFactor + zFilt * (1 - kFilterFactor) ;

     Vector3 filteredAcc = new Vector3(xFilt,yFilt,zFilt);
     return filteredAcc;
     
 }
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 ahmedbenlakhdhar · Dec 06, 2014 at 10:20 PM 0
Share

Thank you for replying, but this script is a bit vague.

What are the initial values of xFilt, yFilt and zFilt?

And is kFilterFactor a constant?

avatar image
0

Answer by Jakew01 · Dec 21, 2014 at 01:04 AM

Try changing the last line to this: transform.position = new Vector3.Slerp(xPos, yPos, -10);

http://docs.unity3d.com/ScriptReference/Vector3.Slerp.html

I hope this helps.

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 ahmedbenlakhdhar · Dec 21, 2014 at 01:44 AM 0
Share

Thank you for your response. Slerp just adds a slow-in and a slows-out to the movement. What I need is a smooth and accurate result at the same time. But I think that after all it has to be balanced.

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

31 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

Related Questions

Smooth Camera/Object Movement 1 Answer

How can I get my camera to reset its position behind the player? 1 Answer

Camera Rendering Skewed Until Transform Adjusted 0 Answers

Crosshair Not Parallel To camera 1 Answer

How Can I Make The Parkour Moves Move Smoothly Instead Of Instantly Teleport? 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