相对于当前旋转对象

时间:2020-08-05 22:48:49

标签: c# unity3d

我有这个脚本,它使立方体像平面一样旋转并旋转,但是我希望它相对于当前旋转旋转。

 int movementSpeed = 2;
    public float rotspeed = 3f;
    public Rigidbody force;
    void FixedUpdate()
    {
        force.AddForce (transform.forward * Time.deltaTime * movementSpeed, ForceMode.VelocityChange);

        if (Input.GetKey("d"))
        {
            force.AddTorque(0, rotspeed, 0, ForceMode.VelocityChange);
        }
        if (Input.GetKey("a"))
        {
            force.AddTorque(0, rotspeed*-1, 0, ForceMode.VelocityChange);
        }
        if (Input.GetKey("w"))
        {
            force.AddTorque(rotspeed, 0, 0, ForceMode.VelocityChange);
        }
        if (Input.GetKey("s"))
        {
            force.AddTorque(rotspeed * -1 , 0, 0, ForceMode.VelocityChange);
        }
    }

如果我要向上旋转飞机,然后按“ a”或“ d”,我想将其向左或向右旋转,但是它会尝试像我在直立时一样旋转。

2 个答案:

答案 0 :(得分:1)

类似于transform.rotationtransform.localRotation,也存在两种不同的方法将力/扭矩施加到刚体上:

与其使用outerWidth而不是

torque:在世界坐标系中的扭矩矢量

,因此要在全球世界中旋转,您宁愿使用Rigidbody.AddTorque,它需要一个

torque局部坐标中的扭矩矢量。

torque相对于其坐标系的Rigidbody添加一个force.AddForceRelative (Vector3.forward * Time.deltaTime * movementSpeed, ForceMode.VelocityChange); if (Input.GetKey("d")) { force.AddTorqueRelative(0, rotspeed, 0, ForceMode.VelocityChange); } if (Input.GetKey("a")) { force.AddTorqueRelative(0, -rotspeed, 0, ForceMode.VelocityChange); } if (Input.GetKey("w")) { force.AddTorqueRelative(rotspeed, 0, 0, ForceMode.VelocityChange); } if (Input.GetKey("s")) { force.AddTorqueRelative(-rotspeed, 0, 0, ForceMode.VelocityChange); }

.proto

答案 1 :(得分:0)

使用AddRelativeTorque代替AddTorque。

相关问题