Unity controls the camera lens to move up, down, left, and right

   

    private float FollowPosx,FollowPosy;
    private float moveAmount=5; //Control the moving speed of the lens

    // Update is called once per frame
    void Update()
    {         if (Input.mousePosition.y > Screen.height * 0.9)//If the mouse position is at the top, move up         {             FollowPosy += moveAmount * Time.deltaTime;


        }
        if (Input.mousePosition.y < Screen.height * 0.1)//If the mouse position is at the bottom, move down
        {             FollowPosy -= moveAmount * Time.deltaTime;         }

        if (Input.mousePosition.x > Screen.width * 0.9)//If the mouse position is on the right, move to the right
        {             FollowPosx += moveAmount * Time.deltaTime;         }         if (Input.mousePosition.x < Screen.width * 0.1) //If the mouse position is on the left, move to the left         {             FollowPosx -= moveAmount * Time.del taTime;         }





        //Call coordinate transformation
        Camera.main.transform.position = new Vector3(FollowPosx, FollowPosy, 0);


    }

The above code detects that when the mouse moves to the edge of the screen, the camera will move in the specified direction

Screen.width is to get the length and width of the screen, and set the range to reach the specified position to trigger the movement effect

Among them, moveAmount controls the moving speed of the lens. At the same time, you can also set moveAmount to change according to time to achieve the effect of acceleration or deceleration.

Time. deltaTime is to control the moving speed of the lens according to the frame rate, the higher the frame rate, the faster the moving speed, and vice versa, the slower.

If you use Time. fixedDeltaTime is a fixed refresh frame rate.

Guess you like

Origin blog.csdn.net/m0_71650342/article/details/130230754
Recommended