基于鼠标移动(包括对角线)围绕点旋转对象 [英] Rotating object around point based on mouse movement including diagonals

查看:27
本文介绍了基于鼠标移动(包括对角线)围绕点旋转对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    
    if( selectedObjs.Count==0&&Input.GetMouseButtonDown(0))
    {
        if (Physics.Raycast(ray,out hit,100, 1 << 9))
        {
            currentPos = hit.point;
        }
    }

    if (mouseClickMode !=3 && mouseClickMode != 4 && !uiMenu_on 
        && Input.GetMouseButton(0))
    {
        float rotX = Input.GetAxis("Mouse X");
        float rotY = -Input.GetAxis("Mouse Y");
        if (selectedObjs.Count > 0)
            currentPos = objectManager.ReturnPos(selectedObjs[0]);
                    
        transform.RotateAround(currentPos, Vector3.up, Time.deltaTime *450* rotX);
        transform.RotateAround(currentPos, Vector3.right, Time.deltaTime*450 * rotY);

   
    }

此旋转先在 x 轴上旋转,然后在 y 轴上旋转.

This rotation is rotating on the x axis and then on the y axis.

但是当鼠标对角移动时,轴不是对角的.当鼠标对角移动时,如何让对象围绕对角轴上的点旋转?

But when the mouse is moved diagonally, the axis is not a diagonal one. How can I have the object rotate around point on a diagonal axis when the mouse is moved diagonally?

推荐答案

使用相机的 transform.TransformDirection 在世界空间中找到鼠标的方向.(注意 float rotY = Input.GetAxis("Mouse Y"); 上没有负数)

Find the mouse's direction in world space using the camera's transform.TransformDirection. (note NO negative on float rotY = Input.GetAxis("Mouse Y");)

使用鼠标的世界方向和相机的前进方向之间的叉积来确定您感兴趣的旋转轴.

Use cross product between the mouse's world direction and the camera's forward to determine the axis you're interested in rotating around.

根据鼠标移动的幅度进行旋转.

Rotate based on the magnitude of the mouse's movement.

总和:

[RequireComponent(typeof(Collider))]
public class TestScript : MonoBehaviour
{
    Vector3 currentPos;
    Camera mainCam;
    
    private void Start()
    {
        mainCam = Camera.main;
    }

    private void Update()
    {
        RaycastHit hit;
        Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);

        if (Input.GetMouseButtonDown(0))
        {
            if (Physics.Raycast(ray, out hit))
            {
                currentPos = hit.point;
            }
        }

        if (Input.GetMouseButton(0))
        {
            float rotX = Input.GetAxis("Mouse X");
            float rotY = Input.GetAxis("Mouse Y");

            Vector3 mouseMove = new Vector3(rotX, rotY, 0f);
            Vector3 mouseWorldDirection = mainCam.transform.TransformDirection(
                    mouseMove);
            Vector3 rotAxis = Vector3.Cross(mouseWorldDirection, 
                    mainCam.transform.forward);

            transform.RotateAround(currentPos, rotAxis, Time.deltaTime * 450f 
                    * mouseMove.magnitude);
        }
    }
}

在您的代码中,它可能如下所示:

In your code, it might look like this:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
if( selectedObjs.Count==0&&Input.GetMouseButtonDown(0))
{
    if (Physics.Raycast(ray,out hit,100, 1 << 9))
    {
        currentPos = hit.point;
    }
}

if (mouseClickMode !=3 && mouseClickMode != 4 && !uiMenu_on 
    && Input.GetMouseButton(0))
{
    float rotX = Input.GetAxis("Mouse X");
    float rotY = Input.GetAxis("Mouse Y");
    if (selectedObjs.Count > 0)
        currentPos = objectManager.ReturnPos(selectedObjs[0]);
                
    Vector3 mouseMove = new Vector3(rotX, rotY, 0f);
    Vector3 mouseWorldDirection = mainCam.transform.TransformDirection(mouseMove);
    Vector3 rotAxis = Vector3.Cross(mouseWorldDirection, mainCam.transform.forward);

    transform.RotateAround(currentPos, rotAxis, Time.deltaTime * 450f 
            * mouseMove.magnitude);
}

这篇关于基于鼠标移动(包括对角线)围绕点旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆