简体   繁体   English

Unity:使用鼠标位置为我的相机绕物体旋转角度

[英]Unity: Using Mouse Position To Make Angles For My Camera To Rotate Around An Object

I am trying to make a mouse-oriented 3d Agar.io. 我正在尝试制作一个面向鼠标的3dAgar.io。 Right now, I am working on the basic movement and camera readjustments. 现在,我正在研究基本的机芯和相机调整。 I want the camera to be able to rotate around an object, but here is the catch: I want to use only the mouse to move the object and camera, and rotate the camera. 我希望相机能够围绕一个对象旋转,但是这里有个要点:我只想使用鼠标来移动对象和相机,然后旋转相机。 I, however, was able to configure the logic of this using the distance formula twice and the law of cosines. 但是,我能够两次使用距离公式和余弦定律来配置其逻辑。 I based this on the origin point (0, 0). 我基于原点(0,0)。 I am positive that there is nothing wrong with the process that I did; 我确信我所做的过程没有错; however, I know that the way it is being outputted is wrong. 但是,我知道它的输出方式是错误的。

Every time you move the mouse left or right, I want the camera to rotate immediately after. 每次向左或向右移动鼠标,我都希望相机在此之后立即旋转。 I tried using a FixedUpdate and LateUpdate event to try to make this happen but both did not succeed. 我尝试使用FixedUpdate和LateUpdate事件来尝试实现此目的,但两者均未成功。 The code I tried using for the camera to rotate is this: 我尝试使相机旋转的代码是这样的:

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

The full code I have (updated): 我拥有的完整代码(已更新):

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

Let me elaborate on the code. 让我详细说明一下代码。 I wanted the camera to face toward the origin when the mouse was the closest to it and vise versa. 当鼠标离它最近时,我希望相机面向原点,反之亦然。 I am having some trouble with this because I don't think I set it up right. 我对此有些麻烦,因为我认为设置不正确。 My attempt at trying to fix this was using reverse logic (ie when the mouse is closer to the origin the camera is the furthest away from the origin [mouse -> object -> camera]. The opposite would be true if the mouse was the furthest from the origin [camera -> object -> mouse].) 我尝试解决此问题的方法是使用反向逻辑(即,当鼠标靠近原点时,摄像头距离原点最远[mouse-> object-> camera]。如果鼠标是原点,则相反。离起点最远的位置[相机->对象->鼠标]。)

Now that you know some of the issues I am having, here is what I need answered (I would prefer you write a solution and tell me how it fixes the problem.): How do I fix the camera rotation so it rotates immediately when the mouse moves, and how can I specify whether the mouse is closer, or not, to the origin? 现在,您知道我遇到的一些问题,这就是我需要解决的问题(我希望您写一个解决方案,并告诉我它如何解决该问题。):如何解决摄像机旋转问题,以便在摄像机旋转时立即旋转鼠标移动,如何指定鼠标是否更靠近原点?

Update: I moved all the code to LateUpdate because that's when you should do your camera algorithms. 更新:我将所有代码移至LateUpdate,因为那是您应该执行相机算法的时间。 I also "tried" to simplify the mouse location part, so it isn't so redundant. 我也“尝试”简化了鼠标位置部分,因此它并不是那么多余。 I also have a link to a screen capture of my project: https://gyazo.com/2f5ee8f94c0bc55bbdeaeafa8a8b35f4 我也有指向我的项目的屏幕截图的链接: https : //gyazo.com/2f5ee8f94c0bc55bbdeaeafafa8a8b35f4

我找到了解决方案:

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

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

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