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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity:使用鼠标位置为我的相机制作角度以围绕对象旋转相关的知识,希望对你有一定的参考价值。

我正在尝试制作一个面向鼠标的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);
    }
}

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

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

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

答案

我找到了解决方案:

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

以上是关于Unity:使用鼠标位置为我的相机制作角度以围绕对象旋转的主要内容,如果未能解决你的问题,请参考以下文章

unity中相机任意位置角度且与鼠标位置无关时Debug.DrawLine的终点参数怎么确定?

带有 Unity 3D 的 C#:当用户移动鼠标时,如何使相机围绕对象移动

在Unity 2d中使用鼠标进行有限的头部转动

Unity相机围绕物体

unity3D:游戏分解之角色移动和相机跟随

unity 怎样正确获取2d世界坐标