简体   繁体   English

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

[英]Rotate an object towards mouse position and fire some bullet

Problem问题

I have a GameObject in my scene and I want that GameObject to rotate so it faces my mouse when I click it.我的场景中有一个游戏对象,我希望该游戏对象旋转,以便在我单击它时面向我的鼠标。 After the rotation it's supposed to move in the direction it is now facing.旋转后,它应该朝着它现在面对的方向移动。 However it almost works.但是它几乎有效。

Example例子

For instance lets say the hypotenuse is: 4.592912 and the adjacent is: 3.042814, then i take MathF.Cos(adjacent/hypotenuse) = 0.6625021, now i take MathF.Acos(0.6625021) * MathF.Rad2Deg = 37.95857例如,假设斜边是:4.592912,相邻的是:3.042814,那么我取 MathF.Cos(adjacent/hypotenuse) = 0.6625021,现在我取 MathF.Acos(0.6625021) * MathF.Rad295 = 8577。

GIF from the scene:现场GIF:

在此处输入图片说明

Code代码

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

Here's a robust method using Mathf.Atan , Rigidibody2D and ConstantForce2D :这是使用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;
        }
    }
}

Here's the scene setup:这是场景设置:

(scale of the parent object is 10) (父对象的比例为 10)

在此处输入图片说明

Result:结果:

在此处输入图片说明

Notes:笔记:

You might be off by +/- 90 degrees, in my example I didn't bother, I just moved the red tip to the proper place, I'll leave that to you as an exercise !您可能偏离了 +/- 90 度,在我的示例中,我没有打扰,我只是将红色尖端移到了正确的位置,我将把它留给您作为练习!

Another thing you can do is to destroy objects that are not visible anymore, should be easy with the methods I used in Camera .您可以做的另一件事是销毁不再可见的对象,使用我在Camera使用的方法应该很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM