简体   繁体   English

如何在 Unity3D 中将方向盘旋转到原始旋转?

[英]How can I rotate a steering wheel to it's original rotation in Unity3D?

I have been searching for an answer for hours, and I cant find the solution.我一直在寻找几个小时的答案,但我找不到解决方案。 I have a script that rotates a steering wheel when the car is turning.我有一个脚本可以在汽车转弯时旋转方向盘。 It has maximum rotation of 120 and a minimum rotation of -120.它的最大旋转为 120,最小旋转为 -120。 I have been trying to rotate with RotateTowards and other Quaternion methods but I can't figure them out.我一直在尝试使用 RotateTowards 和其他四元数方法进行旋转,但我无法弄清楚它们。 How can I make it when the turning angle is not zero and turning keys (a and d) are not pressed, it goes to it's original rotation of 0, 0, 0, at a certain speed?当转动角度不为零并且没有按下转动键(a和d)时,我怎样才能做到,它会以一定的速度进入原来的0、0、0旋转?

Here's my script for the steering wheel:这是我的方向盘脚本:

    if (horizontalInput > 0)
    {
        wheel.transform.Rotate(Vector3.forward*Time.deltaTime*wheelspeed);
        rotations -= wheelspeed;
    }
    else if (horizontalInput < 0)
    {
        wheel.transform.Rotate(-Vector3.forward * Time.deltaTime * wheelspeed);
        rotations += wheelspeed;

    }

And here is my (very bad) script for the minimum and maximum rotation of the steering wheel:这是我的(非常糟糕的)方向盘最小和最大旋转的脚本:

    angle += Input.GetAxis("Horizontal") * Time.deltaTime*10;
    angle = Mathf.Clamp(-120, angle, 120);
    wheel.transform.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
    
   
    angle += Input.GetAxis("Horizontal") * Time.deltaTime * 400;
    angle = Mathf.Clamp(120, 0, angle);
    wheel.transform.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);

You should do something like this for the steering wheel script:你应该为方向盘脚本做这样的事情:

float rotateBack = 0f;
float angle = 0f;

void Update(){
 angle += Input.GetAxis(“Horizontal”) * Time.deltaTime * 10;
 angle = Mathf.Clamp(angle, -120, 120);
 if (Input.GetAxis(“Horizontal”) == 0 && angle != 0f){
  angle += rotateBack * Time.deltaTime;
  if (angle > 0){
   rotateBack = -10f;
  }
  else{
   rotateBack = 10f;
  }
 }
 transform.rotation = Quaternion.Euler(0f, 0f, angle);
}

What this does is setting a variable of angle to the input times a value to increase turning speed.这样做是将一个角度变量设置为输入乘以一个值以增加转动速度。 Then it clamps if so it doesn't go over 120, or under -120.如果是这样,它就会夹紧 go 超过 120 或低于 -120。 Then, if there is no input and the angle isn't zero: it will change the variable by how much to rotate back.然后,如果没有输入并且角度不为零:它将改变变量旋转多少。 The rotate back variable is set to either positive or negative based on which direction the steering wheel needs to be turned.根据方向盘需要转动的方向,将旋转返回变量设置为正值或负值。 The only problem with this script that I could see is the angle not being exactly 0, continuing the if statement.我能看到的这个脚本的唯一问题是角度不完全是 0,继续 if 语句。 If this error affects your game, use the Mathf.Round();如果此错误影响您的游戏,请使用Mathf.Round(); function. function。 (If there are any errors, comment on this post.) (如果有任何错误,请在本帖评论。)

I would do what @ken is doing, but refactor the speed factors and other constants into fields so they are more easily changed and also use Mathf.MoveTowardsAngle to move the angle back to its neutral position.我会做@ken 正在做的事情,但是将速度因子和其他常量重构为字段,以便更容易更改它们,并且还使用Mathf.MoveTowardsAngle将角度移回其中性 position。

[SerializeField] float rotateBackSpeed = 3f; // degrees per second
[SerializeField] float rotateSpeed = 10f;    // degrees per second
[SerializeField] float angle = 0f;           // degrees
[SerializeField] float minAngle = -120f;     // degrees
[SerializeField] float maxAngle = 120f;      // degrees
[SerializeField] float neutralAngle = 0f;    // degrees

void Update()
{
    angle = Mathf.Clamp(angle + Input.GetAxis(“Horizontal”) * rotateSpeed 
            * Time.deltaTime, minAngle, maxAngle);

    if (Mathf.Approximately(0f, Input.GetAxis(“Horizontal”))) 
    {
        angle = Mathf.MoveTowardsAngle(angle, neutralAngle, 
                rotateBackSpeed * Time.deltaTime);
    }

    transform.eulerAngles = angle * Vector3.forward;
}

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

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