当前位置:网站首页>Unity development --- the mouse controls the camera to move, rotate and zoom

Unity development --- the mouse controls the camera to move, rotate and zoom

2022-07-07 22:22:00 Hey, hey, hey, hey, hey

void Update()
{
    float scrollWheel = Input.GetAxis("Mouse ScrollWheel");
    if(scrollWheel != 0)
    {
        Camera_Scale(scrollWheel);
    }
    else if(Input.GetKeyDown(KeyCode.LeftAlt))// Press down Alt key 
    {
        if(Input.GetMouseButton(0))
        {
            Camera_Rotate(); 
        }
    }
    else if(Input.GetMouseButton(2))
    {
        Camera_Move();
    }
}
private void Camera_Scale(float scrollWheel)
{
    scrollWheel = scrollWheel * Time.DeltaTime * 500;
    transform.Translate(Vector3.forward * wheel);
}
private void Camera_Rotate()
{
    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = Input.GetAxis("Mouse Y");
    transform.RotateAround(transform.position, Vector3.up, mouseX * 5);
    transform.RotateAround(transform.position, transform.right, -mouseY * 5);
}
private void Camera_Move()
{
    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = Input.GetAxis("Mouse Y");
    transform.Translate(Vector3.left * mouseX);
    transform.Translate(Vector3.up * mouseY * -1);
}

原网站

版权声明
本文为[Hey, hey, hey, hey, hey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130606206832.html