0
votes

I've been trying to implement a low-pass filter code sample that I found In the Unity Manual into my game but I am having some issues.

Here is the original JavaScript sample found in the Unity Manual:

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;
}

Here is my conversion to C#:

float AccelerometerUpdateInterval = 1.0f / 60.0f;
float LowPassKernelWidthInSeconds = 1.0f;

private float LowPassFilterFactor = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds; // tweakable
private Vector3 lowPassValue = Vector3.zero;

void  Start ()
{
    lowPassValue = Input.acceleration;
}

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

My original code to move my character using the accelerometer was this(no low pass filter):

float speed = 30.0f;
Vector2 dir;
private float accel;

void Start () 
{
    accel = Input.acceleration.x;
}

// Update is called once per frame
void Update () 
{  
    accel = Mathf.MoveTowards (accel, Input.acceleration.x, speed * Time.deltaTime);
    dir = new Vector3(accel, 0);

    // move the object at the velocity defined in speed:
    transform.Translate(dir * speed * Time.deltaTime, 0);
}

This is my attempt to implement the low pass filter to reduce the noise/jerkiness:

float AccelerometerUpdateInterval = 1.0f / 60.0f;
float LowPassKernelWidthInSeconds = 1.0f;

private float LowPassFilterFactor;
private Vector3 lowPassValue = Vector3.zero;

public float speed = 30.0f;

void Start () 
{
    lowPassValue = Input.acceleration;
    LowPassFilterFactor = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds; 
}

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

// Update is called once per frame
void Update () 
{  
    // move the object at the velocity defined in speed:
    transform.Translate(LowPassFilterAccelerometer() * speed * Time.deltaTime, 0);
}

I am getting some errors when I try to compile the script:

error CS1502: The best overloaded method match for `UnityEngine.Mathf.Lerp(float, float, float)' has some invalid arguments

error CS1503: Argument #1' cannot convertUnityEngine.Vector3' expression to type `float'

2
I think error messages are clear. Have you ever read them? Clearly the parameters types are wrong one of Lerp method that you used. For the second one, you try to cast Vector3 type to float which seems not possible. - Soner Gönül
It says that Mathf.Lerp only takes float arguments .. but why would the sample from the Unity Manual use Vector3's ? - LooMeenin

2 Answers

1
votes

The example code may be wrong. Use Vector3.Lerp() instead. A full list of Lerps is here.

0
votes

The error message tells you all you need to know. Look at the signature for UnityEngine.Mathf.Lerp(float, float, float)

One of the parameters you're passing to it isn't a float.