- Home /
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);
}
}
}
If you can't trust each individual input value, you have to either
calculate some kind of average of past N inputs
require at least N times of consecutive inputs with the same value before changing direction
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 ?
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);
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.
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.
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!
Your answer
Follow this Question
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