-1

我最近开始开发我的第一个自上而下的 2d 游戏。我的问题是不知道如何让子弹在激活子弹时到达鼠标所面对的位置。我有一个面部鼠标功能,如此处所示

void faceMouse()
{

    Vector3 mousePosition = Input.mousePosition;
    mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

    Vector2 direction = new Vector2(
        mousePosition.x - transform.position.x,
        mousePosition.y - transform.position.y);

    transform.up = direction;
}

但是,如果要在鼠标所在的位置进行拍摄,我不确定如何将其合并。提前致谢!

4

1 回答 1

0

您可以尝试Quaternion.LookRotation()根据向前和向上的向量(文档)创建旋转。然后,您需要将该旋转分配给您的对象。我使用它是这样的:

cursorPos = mainCamera.ScreenToWorldPoint(Input.mousePosition);
var forwardDirection = transform.position - cursorPos;
//Vector3.forward because is the z axis which is up/down in 2D
Quaternion playerRotation = Quaternion.LookRotation(forwardDirection, Vector3.forward);
transform.rotation = playerRotation;
于 2019-03-13T11:00:19.823 回答