将对象旋转到鼠标位置并发射一些子弹

Rotate an object towards mouse position and fire some bullet

问题

我的场景中有一个游戏对象,我希望该游戏对象旋转,以便在我单击它时它面向我的鼠标。旋转后,它应该朝它现在面对的方向移动。但是它几乎可以工作。

例子

例如假设斜边是:4.592912,相邻的是:3.042814,那么我取 MathF.Cos(adjacent/hypotenuse) = 0.6625021,现在我取 MathF.Acos(0.6625021) * MathF.Rad2Deg = 37.95857

现场GIF:

代码

float mouseX, mouseY;
float playerX, playerY;
float squaredDeltaX, squaredDeltaY;
float hypotenuse;

int dirX, dirY;

float rotation; 

bool mouseClicked = false;

void Update () {
    UpdateInputs();

    if (mouseClicked)
    {
        CalcDistance(playerX,playerY,mouseX,mouseY);
        SetRotation(squaredDeltaX, squaredDeltaY, hypotenuse);
    }

}

void UpdateInputs()
{
    Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
    mouseX = pos.x;
    mouseY = pos.y;

    playerX = transform.position.x;
    playerY = transform.position.y;

    mouseClicked = Input.GetMouseButtonDown(0);
}

void CalcDistance(float x, float y, float x1, float y1)
{
    dirX = (x - x1) >= 0 ? 1 : -1;
    dirY = (y - y1) >= 0 ? 1 : -1;

    squaredDeltaX = (x - x1) * (x - x1);
    squaredDeltaY = (y - y1) * (y - y1);
    hypotenuse = Mathf.Sqrt(squaredDeltaX + squaredDeltaY);

    print("squaredDelta: " + squaredDeltaX + ", " + squaredDeltaY);
    print("Hypotenuse: " + hypotenuse);
    print("delta: " + Mathf.Sqrt(squaredDeltaX)*dirX + ", " + Mathf.Sqrt(squaredDeltaY)*dirY);
}

void SetRotation(float opposite, float adjacent, float hypotenuse)
{
    float tempOpposite = Mathf.Sqrt(opposite);
    float tempAdjacent = Mathf.Sqrt(adjacent);

    rotation = Mathf.Acos(Mathf.Cos(tempOpposite / hypotenuse));
    print("MathF.Cos: " + rotation);
    rotation *= Mathf.Rad2Deg * dirX;
    print("MathF.Acos: " + rotation);
    rotation = dirY != -1 ? rotation + 180 : rotation;
    print("angle: " + rotation + "*");

    transform.eulerAngles = new Vector3(1,1,rotation);
}

这是使用 Mathf.AtanRigidibody2DConstantForce2D 的可靠方法:

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    private Camera _camera;
    private GameObject _find;

    private void OnEnable()
    {
        _find = GameObject.Find("New Sprite");
        _camera = Camera.main;
    }

    private void Update()
    {
        // find the vector between cannon and mouse position
        var p1 = _camera.ScreenToViewportPoint(Input.mousePosition);
        var p2 = _camera.WorldToViewportPoint(_find.transform.position);
        var p3 = p2 - p1;

        // rotate cannon to mouse position
        var angle = Mathf.Atan2(p3.y, p3.x) * Mathf.Rad2Deg;
        _find.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

        // throw a projectile on mouse down
        if (Input.GetMouseButtonDown(0))
        {
            var clone = Instantiate(_find);

            var rb = clone.AddComponent<Rigidbody2D>();
            rb.gravityScale = 0;

            var force = clone.AddComponent<ConstantForce2D>();
            force.relativeForce = Vector2.left * 5.0f;
        }
    }
}

场景设置如下:

(父对象的比例为10)

结果:

备注:

你可能会偏离 +/- 90 度,在我的示例中我没有费心,我只是将红色尖端移动到适当的位置,我会把它留给你作为练习!

您可以做的另一件事是销毁不再可见的对象,使用我在 Camera.

中使用的方法应该很容易