以精确的角度发射弹丸

Shoot projectile at exact angle

我正在尝试开发一种简单的射击机制,它根据射击者的旋转角度实例化 'shot'。我这里的问题是,即使射手的方向为 0*,射击的角度为 45*(这就是我猜的问题,因为当射手的方向为 45* 时,射击的角度正好90*).

射手角度(0,0,0)

射手角度(0,0,45)

注意-球总是从黑色扁平圆柱体的中心发射。

所需代码:

public class ShotMoveScript : MonoBehaviour {
    public static float xForce;
    public Transform shott;
    void Update () {
        if(Input.GetKey(KeyCode.Q))
        {
            transform.Rotate(Vector3.forward, 5f);
        }

        if(Input.GetKey(KeyCode.E))
        {
            transform.Rotate(Vector3.forward, -5f);
        }

        if(Input.GetKey(KeyCode.Space))
        {
            xForce += 0.2f;
        }

        if(Input.GetKeyUp(KeyCode.Space))
        {
            Instantiate(shott, transform.position, transform.rotation);
        }

    }
}

附加到实例化球的脚本:

public class MovementScript : MonoBehaviour {
    void Update () {
        Rigidbody2D rb;
        rb = GetComponent<Rigidbody2D> ();
        rb.gravityScale = 0.8f;
        transform.Translate( new Vector3(1,1,0) * ShotMoveScript.xForce * Time.deltaTime, Space.Self);
    }
}

在使用第三个参数 rotation 实例化球时旋转球(参见 reference)。 之后应用指向 (1, 1) 的向量,因此球将沿该方向移动 相对于他的局部旋转

要么通过Quaternion.identity as the third parameter to Instantiate方法,要么让小球沿(1, 0)方向移动。