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 Bamiebal · May 29, 2015 at 05:37 AM · movementlagarduinosensor

How do i Smooth ingame movement with flickering sensor input?

Hello,

Im working on a game you can control with a accelerometer the game is working with an accelerometer trough arduino unfortunately the player really flickers to the right or left. The player also gets stuck in walls a bit. To solve I wanted to use forces instead of translations so movement would be smoother and the player couldn't get stuck but after trying for hours i can't seem to figure out how to do this.

If anybody here has a (better) solutions to this problem i would be very grateful for any input on how to do this.

below the code I'm using

using UnityEngine; using System.Collections; using System.IO.Ports;

 public class NewBehaviourScript : MonoBehaviour {
     
     SerialPort sp = new SerialPort( "COM5", 9600);
     public float speed;
     private float amountToMove;
     
     void Start () {
         
         sp.Open ();
         sp.ReadTimeout = 1;
         
     }
     
     void FixedUpdate()
     {
         amountToMove = speed * Time.deltaTime;
         
         if (sp.IsOpen) {
             try {
                 Movement (sp.ReadByte());
                 print (sp.ReadByte());
             }
             catch (System.Exception)
             {
                 
             }
         }
     }
     void Movement (int Direction)
     {
         if (Direction == 49){
             transform.Translate (Vector3.left * speed * Time.deltaTime , Space.World);
         }
         
         if (Direction == 50) {
             transform.Translate (Vector3.right * speed * Time.deltaTime , Space.World);
         }
     }
     }
 


Comment
Add comment · Show 5
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 NoseKills · May 29, 2015 at 09:35 PM 1
Share

If you can't trust each individual input value, you have to either

  1. calculate some kind of average of past N inputs

  2. require at least N times of consecutive inputs with the same value before changing direction

  3. add to or subtract from a value based on input and do movement based on its value (kinda the same as #1 but might be easier to do)

What are the possible input values and what kind of functionality are you looking for? Is staying still an option or are you always moving ?

avatar image apple92 · May 29, 2015 at 09:45 PM 1
Share

Depending on how much you want to reduce the flickering.. Wouldn't it be enough to just constantly Lerp?

I mean like:

 transform.position = Vector3.Lerp(transform.position, Vector3.left * speed, Time.deltaTime * SmoothFactor);

avatar image NoseKills · May 31, 2015 at 12:39 AM 0
Share

Lerp could be used too, but it wouldn't really help you solve the problem of moving to left and right in consecutive frames.

Lerp takes a target position as the second parameter (not direction as your example seems to imply). You would still have to somehow make sure the target position is not repeatedly on different sides of the object in consecutive frames.

avatar image apple92 · May 31, 2015 at 07:49 AM 0
Share

Hah.. $$anonymous$$y bad. I automatically though of an endless runner alike game, that will clamp at max input Value anyway. (Friend of $$anonymous$$e did that once with an arduina accelometer (altough on Flash). But that would only mean that Bamiebal needs to construct a vector3 with transform.position and the direction Vector3 and then use this new Vector as Target Lerp. Shouldn't be that hard.

avatar image Bamiebal · Jun 02, 2015 at 11:22 AM 0
Share

Thanks the lerping helped a lot. I also lowered the data output frequency from the arduino and smoothed the sensor data in an array so it's more s$$anonymous$$dy. In combination with the lerp it totally worked out!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I connect Invensens MPU-9150 SDK with Unity? 0 Answers

FPS Movement Incredibly Laggy 0 Answers

First Person Controller movement Lag? 1 Answer

Character Controller has Input Lag. How do I Fix it? 0 Answers

touch control code optimization 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