Unity2D - 旋转左轮以移动汽车



我正在使用Unity2D进行简单的汽车/自行车物理游戏。我希望当我按下向右或向左箭头时,车轮精灵旋转,这样汽车就会移动。

这是我的代码:

float move=Input.GetAxis("Horizontal");
        if (Input.GetKey(KeyCode.RightArrow))
        {
            rigidbody2D.velocity = new Vector2(move*10,rigidbody2D.velocity.y);

        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {

            rigidbody2D.velocity = new Vector2(move * 10, rigidbody2D.velocity.y);
        }

但这只是"推动"车轮,而不是旋转,如果汽车在空中,你仍然可以移动它......我需要旋转轮子,而不是推动它。谁能帮忙?

速度

只是朝着一个方向移动,就像你在脚本中看到的那样。 另一方面,角度速度是旋转。尝试使用刚性车身2D.angularVelocity,看看会发生什么。

这个简单的代码将旋转 2d 对象。自旋的速度取决于所选物体的速度有多快。

#pragma strict
var power : float; //the engine power applied to the wheel
var car : GameObject; //the object whose velocity you are calculating
function Start () {
}
function Update () {
 var wheelpower = car.rigidbody2D.velocity.x * power; //velocity of "car" * engine power
if(Input.GetKey(KeyCode.D)){
    transform.Rotate(0, 0, -wheelpower);}
if(Input.GetKey(KeyCode.A)){
    transform.Rotate(0, 0, wheelpower);}

}

最新更新