C# 找出旋转的差异,作为随时间变化的平均值#

C# 找出旋转的差异,作为随时间变化的平均值#,c#,unity3d,euler-angles,C#,Unity3d,Euler Angles,我有一个船轮,玩家可以旋转。我当前记录了角度变化,该变化被添加到总角度中。然后我就可以计算出车轮转动了多少次。这一切都很好 void Update() { //Record Amount of Wheel Turns currentAngle = wheelRectTransform.transform.rotation.eulerAngles.z; angleChange = Mathf.DeltaAngl

我有一个船轮,玩家可以旋转。我当前记录了角度变化,该变化被添加到总角度中。然后我就可以计算出车轮转动了多少次。这一切都很好

void Update()
        {
            //Record Amount of Wheel Turns
            currentAngle = wheelRectTransform.transform.rotation.eulerAngles.z;
            angleChange = Mathf.DeltaAngle(currentAngle, previousAngle);

            if ((Mathf.Abs(angle + angleChange) / 360) <=   wheelTurnsUntilRudderAtMaxAngle)
            {            
               angle += angleChange;               
            } 

            totalWheelTurns = angle / 360;
            shipStats.RudderAngle = totalWheelTurns / wheelTurnsUntilRudderAtMaxAngle;

            previousAngle = currentAngle;
        }
这不起作用,它给了我相同的反馈,因为我只是使用“angleChange”,它可以是0

谢谢


Jim

检测车轮方向不需要求平均值。只需使用新旧变量即可确定方向盘的运行方向

 void Start()
 {
        StartCoroutine(wheelDIRCalculator(_wheelTransform));
 }


bool continueWheelCalculation = false;

 IEnumerator wheelDIRCalculator(Transform wheelTransform)
 {
     yield return null;

     continueWheelCalculation = true;

     float oldZAngle = 0;
     float newZAngle = 0;
     bool isIdle = false;

     oldZAngle = Mathf.Abs(wheelTransform.rotation.eulerAngles.z);


     while (continueWheelCalculation)
     {

         //Get new rotation
         newZAngle = Mathf.Abs(wheelTransform.rotation.eulerAngles.z);

         if (oldZAngle < newZAngle)
         {
             isIdle = false;
             oldZAngle = newZAngle;
             Debug.Log("Left");

             //Do something 

         }
         else if (oldZAngle > newZAngle)
         {
             isIdle = false;
             oldZAngle = newZAngle;
             Debug.Log("Right");

             //Do something 

         }
         else if (!isIdle)
         {
             isIdle = true;
             oldZAngle = newZAngle;
             Debug.Log("Idle");

             //Do something 

         }


         yield return null;
     }
 }

 void stopWheelDIRCalculator()
 {
    continueWheelCalculation = false;
 }
void Start()
{
Start例程(wheelDIRCalculator(_wheelTransform));
}
布尔连续计算=假;
IEnumerator wheelDIRCalculator(变换wheelTransform)
{
收益返回空;
连续计算=真;
浮点数=0;
float-newzange=0;
bool-isIdle=false;
Oldzange=Mathf.Abs(车轮变换、旋转、欧拉格尔斯、z);
while(继续计算)
{
//获得新的轮换
newzange=Mathf.Abs(车轮变换、旋转、欧拉格尔斯、z);
如果(旧赞歌<新赞歌)
{
isIdle=假;
奥尔德桑格尔=纽桑格尔;
Debug.Log(“左”);
//做点什么
}
else if(oldZAngle>newZAngle)
{
isIdle=假;
奥尔德桑格尔=纽桑格尔;
Debug.Log(“右”);
//做点什么
}
否则如果(!isIdle)
{
isIdle=true;
奥尔德桑格尔=纽桑格尔;
Debug.Log(“空闲”);
//做点什么
}
收益返回空;
}
}
void stopWheelDIRCalculator()
{
连续计算=假;
}

最后,我使用了Lerp来获得平均值

    public enum WheelDirection { Left, Right, Still }

    [Header("Average Wheel Direction")]
    public float lerpTime = 0.5f; // How many seconds average over
    private WheelDirection wheelDirection; 
    private float wheelDeltaX;
    private float wheelDirectionInterpolated;
    private float currentLerpTime;

 void Update()
    {
        // Get DeltaX of objects rotation 
        // I'm interested in the Z axis, but you could change this to x/y           
        float currentAngle = transform.rotation.eulerAngles.z;
        wheelDeltaX = (currentAngle - previousAngle);

        // Reduce the Lerp percentage
        currentLerpTime += Time.deltaTime;
        if (currentLerpTime > lerpTime)
        {
            currentLerpTime = lerpTime;
        }
        float perc = currentLerpTime / lerpTime;

        // Lerp!
        wheelDirectionInterpolated = (Mathf.Lerp(wheelDirectionInterpolated, wheelDeltaX, perc));

        // Wheel has finished rotating, so reset the Lerp
        if (wheelDirectionInterpolated == 0) currentLerpTime = 0;

        // Store Direction in an Enum
        if (wheelDirectionInterpolated > 0) wheelDirection = WheelDirection.Left;
        if (wheelDirectionInterpolated < 0) wheelDirection = WheelDirection.Right;
        if (wheelDirectionInterpolated == 0) wheelDirection = WheelDirection.Still;

        //This should always be at the end
        previousAngle = currentAngle;
    }
public enum WheelDirection{Left,Right,Still}
[收割台(“平均车轮方向”)]
公共浮动时间=0.5f;//平均超过多少秒
私家车方向;
私人浮动税;
私用浮轮方向盘;
私人浮动时间;
无效更新()
{
//获取对象旋转的增量
//我对Z轴感兴趣,但是你可以把它改成x/y
float currentAngle=transform.rotation.eulerAngles.z;
wheelDeltaX=(当前角度-先前角度);
//降低Lerp百分比
currentLerpTime+=Time.deltaTime;
如果(currentLerpTime>lerpTime)
{
currentLerpTime=lerpTime;
}
float perc=currentLerpTime/lerpTime;
//勒普!
wheelDirectionInterpolated=(Mathf.Lerp(wheelDirectionInterpolated,wheelDeltaX,perc));
//车轮已完成旋转,因此请重置Lerp
如果(wheelDirectionInterpolated==0)CurrentLerOptime=0;
//在枚举中存储方向
如果(wheelDirectionInterpolated>0)wheelDirection=wheelDirection.Left;
如果(wheelDirectionInterpolated<0)wheelDirection=wheelDirection.Right;
如果(wheelDirectionInterpolated==0)wheelDirection=wheelDirection.Still;
//这应该总是在最后
前一角度=当前角度;
}

由于我正在制作一些动作的原型,整个代码现在乱七八糟。但我把它放在这里给你。我目前正在使用某种Lerp来确定方向,它并不完美,但可以。是的,z方向。没有刚体。
 void Start()
 {
        StartCoroutine(wheelDIRCalculator(_wheelTransform));
 }


bool continueWheelCalculation = false;

 IEnumerator wheelDIRCalculator(Transform wheelTransform)
 {
     yield return null;

     continueWheelCalculation = true;

     float oldZAngle = 0;
     float newZAngle = 0;
     bool isIdle = false;

     oldZAngle = Mathf.Abs(wheelTransform.rotation.eulerAngles.z);


     while (continueWheelCalculation)
     {

         //Get new rotation
         newZAngle = Mathf.Abs(wheelTransform.rotation.eulerAngles.z);

         if (oldZAngle < newZAngle)
         {
             isIdle = false;
             oldZAngle = newZAngle;
             Debug.Log("Left");

             //Do something 

         }
         else if (oldZAngle > newZAngle)
         {
             isIdle = false;
             oldZAngle = newZAngle;
             Debug.Log("Right");

             //Do something 

         }
         else if (!isIdle)
         {
             isIdle = true;
             oldZAngle = newZAngle;
             Debug.Log("Idle");

             //Do something 

         }


         yield return null;
     }
 }

 void stopWheelDIRCalculator()
 {
    continueWheelCalculation = false;
 }
    public enum WheelDirection { Left, Right, Still }

    [Header("Average Wheel Direction")]
    public float lerpTime = 0.5f; // How many seconds average over
    private WheelDirection wheelDirection; 
    private float wheelDeltaX;
    private float wheelDirectionInterpolated;
    private float currentLerpTime;

 void Update()
    {
        // Get DeltaX of objects rotation 
        // I'm interested in the Z axis, but you could change this to x/y           
        float currentAngle = transform.rotation.eulerAngles.z;
        wheelDeltaX = (currentAngle - previousAngle);

        // Reduce the Lerp percentage
        currentLerpTime += Time.deltaTime;
        if (currentLerpTime > lerpTime)
        {
            currentLerpTime = lerpTime;
        }
        float perc = currentLerpTime / lerpTime;

        // Lerp!
        wheelDirectionInterpolated = (Mathf.Lerp(wheelDirectionInterpolated, wheelDeltaX, perc));

        // Wheel has finished rotating, so reset the Lerp
        if (wheelDirectionInterpolated == 0) currentLerpTime = 0;

        // Store Direction in an Enum
        if (wheelDirectionInterpolated > 0) wheelDirection = WheelDirection.Left;
        if (wheelDirectionInterpolated < 0) wheelDirection = WheelDirection.Right;
        if (wheelDirectionInterpolated == 0) wheelDirection = WheelDirection.Still;

        //This should always be at the end
        previousAngle = currentAngle;
    }