相对于游戏对象的角度增加力

时间:2018-10-13 13:11:01

标签: unity3d

我正在尝试在Unity2D中创建月球着陆器,但在某个方向(主要是火箭所面对的方向)上增加推力时遇到了问题。目前,即使船成90度角,它也将始终保持垂直状态。

    public float thrust;
private Rigidbody2D rocket;
public int rotationSpeed;
// Use this for initialization
void Start()
{
    rocket = gameObject.GetComponent<Rigidbody2D>();
}

void Update()
{

    if (Input.GetButton("Horizontal"))
    {
        if (rocket.transform.rotation.z < 90 && rocket.transform.rotation.z > -90)
        {
            Debug.Log(rocket.transform.rotation.z);
            float angle = Input.GetAxis("Horizontal");
            if (angle > 0)
            {
                rocket.transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);

            }
            else
            {
                rocket.transform.Rotate(Vector3.back * rotationSpeed * Time.deltaTime);

            }
        }
    }

}

void FixedUpdate()
{
    if (Input.GetButton("Vertical"))
    {

        Debug.Log(rocket.transform.rotation.z);
        float angle = Input.GetAxis("Vertical");
        if (angle > 0)
        {
            rocket.AddRelativeForce(Vector3.forward * 10);

        }

    }
}

1 个答案:

答案 0 :(得分:0)

尝试

`rocket.AddForce(Vector3.up*10, ForceMode.Impulse);`

代替