简体   繁体   English

如何将JavaScript(UnityScript)转换为C#并实现低通滤波器?

[英]How to convert JavaScript(UnityScript) to C# and implement a low pass filter?

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. 我一直在尝试将我在Unity手册中找到的低通滤波器代码示例实现到我的游戏中,但是遇到了一些问题。

Here is the original JavaScript sample found in the Unity Manual: 这是在Unity手册中找到的原始JavaScript示例:

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#: 这是我对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 错误CS1502:UnityEngine.Mathf.Lerp(float,float,float)的最佳重载方法匹配具有一些无效的参数

error CS1503: Argument #1' cannot convert UnityEngine.Vector3' expression to type `float' 错误CS1503:参数#1' cannot convert UnityEngine.Vector3'表达式#1' cannot convert为类型'float'

The example code may be wrong. 示例代码可能是错误的。 Use Vector3.Lerp() instead. 使用Vector3.Lerp()代替。 A full list of Lerps is here . 完整的勒普清单在这里

The error message tells you all you need to know. 错误消息告诉您所有您需要了解的内容。 Look at the signature for UnityEngine.Mathf.Lerp(float, float, float) 查看UnityEngine.Mathf.Lerp(float, float, float)的签名

One of the parameters you're passing to it isn't a float . 您要传递给它的参数之一不是float

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM