将物体旋转到鼠标位置并发射一些子弹

时间:2017-10-04 13:15:50

标签: c# unity3d rotation mouse

问题

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

示例

例如,假设斜边是:4.592912,相邻是:3.042814,然后我采取MathF.Cos(相邻/斜边)= 0.6625021,现在我采取MathF.Acos(0.6625021)* MathF.Rad2Deg = 37.95857

来自现场的GIF:

enter image description here

代码

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);
}

1 个答案:

答案 0 :(得分:1)

以下是使用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)

enter image description here

<强>结果:

enter image description here

备注:

你可能会偏离+/- 90度,在我的例子中我并没有打扰,我只是把红色的尖端移到了正确的位置,我会把它留给你作为练习! / p>

您可以做的另一件事是销毁不再可见的对象,使用我在Camera中使用的方法应该很容易。