简体   繁体   中英

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. 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). 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. 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].)

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. 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

我找到了解决方案:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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