Unity:使用鼠标位置为我的相机制作角度以围绕对象旋转

时间:2017-12-14 05:30:06

标签: c# unity3d camera cosine mouse-position

我正在尝试制作面向鼠标的3d Agar.io.现在,我正在研究基本机芯和相机重新调整。我希望相机能够围绕一个物体旋转,但这里有一个问题:我只想用鼠标移动物体和相机,然后旋转相机。然而,我能够使用距离公式两次和余弦定律来配置它的逻辑。我基于原点(0,0)。我很肯定我所做的这个过程并没有错;但是,我知道它的输出方式是错误的。

每次向左或向右移动鼠标时,我都希望相机立即旋转。我尝试使用FixedUpdate和LateUpdate事件来尝试实现这一点,但两者都没有成功。我尝试用于相机旋转的代码是:

void LateUpdate ()
{
    // Move Camera
    transform.position = new Vector3(camX, 10.5f, camZ);

    //Rotate Camera
    //transform.eulerAngles = getRotation;
    //transform.eulerAngles = Vector3.MoveTowards(currentRotation, getRotation, speed * Time.deltaTime);
}

我的完整代码(已更新):

public class CameraController : MonoBehaviour {

    public float speed;
    public GameObject player;

    private Ray ray;
    private RaycastHit hit;
    private Vector3 mousePos;

    private float camX;
    private float camZ;

    private float getA;
    private float getB;
    private float getC;

    private float getAngle;
    private Vector3 currentRotation;
    private Vector3 getRotation;

    void Start ()
    {
    }

    void FixedUpdate()
    {

    }
    void LateUpdate ()
    {
        if (PlayerController.movedPlayer)
        {
            if (Physics.Raycast(ray, out hit))
            {
                mousePos = new Vector3(hit.point.x, 1.3f, hit.point.z);
            }

            // MouseZ
            if (mousePos.z >= player.transform.position.z)
            {
                // Camera Follow Z+
                if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
                    player.transform.position.z >= player.transform.position.x)
                {
                    camZ = player.transform.position.z - 10;
                    camX = 0;
                }
                else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
                    player.transform.position.z >= -player.transform.position.x)
                {
                    camZ = player.transform.position.z - 10;
                    camX = 0;
                }
                else if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
                    player.transform.position.z < player.transform.position.x)
                {
                    camX = player.transform.position.x - 10;
                    camZ = 0;
                }
                else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
                    player.transform.position.z < -player.transform.position.x)
                {
                    camX = player.transform.position.x + 10;
                    camZ = 0;
                }

                // Camera Follow Z-
                if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
                -player.transform.position.z >= player.transform.position.x)
                {
                    camZ = player.transform.position.z + 10;
                    camX = 0;
                }
                else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
                    -player.transform.position.z >= -player.transform.position.x)
                {
                    camZ = player.transform.position.z + 10;
                    camX = 0;
                }
                else if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
                    -player.transform.position.z < player.transform.position.x)
                {
                    camX = player.transform.position.x - 10;
                    camZ = 0;
                }
                else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
                    -player.transform.position.z < -player.transform.position.x)
                {
                    camX = player.transform.position.x + 10;
                    camZ = 0;
                }
            }
            //PlayerZ
            else if (mousePos.z < player.transform.position.z)
            {
                // Camera Follow Z+
                if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
                    player.transform.position.z >= player.transform.position.x)
                {
                    camZ = player.transform.position.z + 10;
                    camX = 0;
                }
                else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
                    player.transform.position.z >= -player.transform.position.x)
                {
                    camZ = player.transform.position.z + 10;
                    camX = 0;
                }
                else if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
                    player.transform.position.z < player.transform.position.x)
                {
                    camX = player.transform.position.x + 10;
                    camZ = 0;
                }
                else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
                    player.transform.position.z < -player.transform.position.x)
                {
                    camX = player.transform.position.x - 10;
                    camZ = 0;
                }

                // Camera Follow Z-
                if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
                -player.transform.position.z >= player.transform.position.x)
                {
                    camZ = player.transform.position.z - 10;
                    camX = 0;
                }
                else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
                    -player.transform.position.z >= -player.transform.position.x)
                {
                    camZ = player.transform.position.z - 10;
                    camX = 0;
                }
                else if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
                    -player.transform.position.z < player.transform.position.x)
                {
                    camX = player.transform.position.x + 10;
                    camZ = 0;
                }
                else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
                    -player.transform.position.z < -player.transform.position.x)
                {
                    camX = player.transform.position.x - 10;
                    camZ = 0;
                }
            }

            // Make Triangle
            getA = mousePos.x - player.transform.position.x;
            getB = mousePos.z - player.transform.position.z;
            getC = Mathf.Sqrt(Mathf.Pow((getA), 2) + Mathf.Pow((getB), 2));

            // Get Rotating Angle.
            if (getA == 0 && getB >= 0)
            {
                getAngle = 0;
            }
            else if (getA == 0 && getB < 0)
            {
                getAngle = 180;
            }
            else if (getB == 0 && getA >= 0)
            {
                getAngle = 90;
            }
            else if (getB == 0 && getA < 0)
            {
                getAngle = 270;
            }
            else
            {
                if (getA > 0)
                {
                    getAngle = Mathf.Acos((Mathf.Pow((getB), 2) + Mathf.Pow((getC), 2) - Mathf.Pow((getA), 2)) /
                        (2 * (getB) * (getC))) * Mathf.Rad2Deg;
                }
                else if (getA < 0)
                {
                    getAngle = -Mathf.Acos((Mathf.Pow((getB), 2) + Mathf.Pow((getC), 2) - Mathf.Pow((getA), 2)) /
                        (2 * (getB) * (getC))) * Mathf.Rad2Deg;
                }
                //getAngle = Mathf.Asin(getA * Mathf.Sin(90) / getC);
                //Debug.Log("A: " + getA + " B: " + getB + " C: " + getC + " Angle: " + getAngle);
            }

            // Set Rotating Angle
            currentRotation = transform.eulerAngles;
            getRotation = new Vector3(45f, getAngle, 0f);
        }
        // Move Camera
        transform.position = new Vector3(camX, 10.5f, camZ);

        //Rotate Camera
        //transform.eulerAngles = getRotation;
        transform.eulerAngles = Vector3.MoveTowards(currentRotation, getRotation, speed * Time.deltaTime);
    }
}

让我详细说明代码。当鼠标离它最近时,我希望相机朝向原点,反之亦然。我遇到了一些麻烦,因为我不认为我把它设置得正确。我尝试解决此问题的尝试是使用反向逻辑(即当鼠标离原点较近时,相机离原点最远[鼠标 - >对象 - >相机]。如果鼠标相反则相反距离原点[相机 - &gt;对象 - &gt;鼠标]最远。)

既然你知道我遇到的一些问题,这就是我需要回答的问题(我希望你写一个解决方案并告诉我它是如何解决问题的。):如何修复相机旋转以使其旋转鼠标移动时立即,如何指定鼠标是否更接近原点?

更新:我将所有代码移至LateUpdate,因为您应该使用相机算法。我也试过&#34;为了简化鼠标定位部分,所以它不是那么多余。我还有一个链接到我的项目的屏幕截图:https://gyazo.com/2f5ee8f94c0bc55bbdeaeafa8a8b35f4

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

transform.rotation = Quaternion.RotateTowards(transform.rotation, getRotation, speed * Time.deltaTime);